April 7, 2009 at 1:12 am
I need your help to find out the query for 'As of Date' stock from inventory table. Out inventory history table has the following structure
cmp_id,
pkg_id
tran_type
itm_code
itm_num
itm_color
itm_size
lot_id
palet_id
status
doc_id
doc_date
whs_id
loc_id
pkg_qty
itm_qty
process_id.
It is the history table, every stock movement would be updated here or append as a record. By using this, find out the sum of Pkg qty for the specified date in a customer.
For expample, 28th March a customer, who named as GSI, received 100 items, 30th March 25 items are allocated . 2nd April he received 50 items and 5th March 80 items has allocated to him. Now the total stock avail has been 45. In Stock Inquiry page, if enter 31st Mar in 'As of Date' field, Avail Qty should show 75
Anybody help me to write a query for this. I tried to write it, but failed.
Please help me as soon as possible
April 7, 2009 at 8:58 am
I think this will work (please note how I supplied some test data):
DECLARE @inventory_history table(itm_code INT, tran_type VARCHAR(15), itm_qty INT, doc_date datetime)
DECLARE @date DATETIME
SET @date = '3/31/2009'
INSERT INTO @inventory_history (
itm_code,
tran_type,
itm_qty,
doc_date
)
SELECT
1,
'Received',
100,
'3/28/09'
UNION ALL
SELECT
1,
'Allocated',
25,
'3/30/09'
UNION ALL
SELECT
1,
'Received',
50,
'4/2/09'
UNION ALL
SELECT
1,
'Allocated',
80,
'4/05/09'
SELECT
itm_code,
SUM(CASE WHEN tran_type = 'Received' THEN itm_qty ELSE itm_qty * -1 END) AS quantity
FROM
@inventory_history
WHERE
doc_date <= @date
GROUP BY
itm_code
This assumes that all transactions are stored in positive numbers so I need to reverse the sign on all other transactions other than receipts. You should be able to figure out the rest.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
April 7, 2009 at 11:29 pm
Thank you for your support, i ll try this qry, touch you back
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply