SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


'As Of Date' Stock -


'As Of Date' Stock -

Author
Message
rajanv-1062303
rajanv-1062303
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19436 Visits: 14900
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
rajanv-1062303
rajanv-1062303
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 3
Thank you for your support, i ll try this qry, touch you back
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search