Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

'As Of Date' Stock - Expand / Collapse
Author
Message
Posted Tuesday, April 7, 2009 1:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 7, 2009 11:27 PM
Points: 2, Visits: 3
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
Post #691738
Posted Tuesday, April 7, 2009 8:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:38 PM
Points: 11,314, Visits: 13,101
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

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

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
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #692125
Posted Tuesday, April 7, 2009 11:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 7, 2009 11:27 PM
Points: 2, Visits: 3
Thank you for your support, i ll try this qry, touch you back
Post #692765
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse