I have a stock Ledger table like this:
item date time in_qty out_qty in_Price out_Price
ABC 01-April-2012 00:00:00 200 750.00
ABC 05-April-2012 00:00:00 100 800.00
ABC 10-April-2012 00:00:00 50 700.00
ABC 16-April-2012 00:00:00 75 800.00
ABC 25-April-2012 00:00:00 175 850.00
XYZ 02-April-2012 00:00:00 150 350.00
XYZ 08-April-2012 00:00:00 120 380.00
XYZ 12-April-2012 00:00:00 80 370.00
XYZ 24-April-2012 00:00:00 80 330.00
XYZ 24-April-2012 00:00:00 90 340.00
LPQ 26-April-2012 00:00:00 70 240.00
LPQ 27-April-2012 00:00:00 30 230.00
I need the value of the inventory for each item in FIFO (First in first out) meaning the first purchased item should be consumed first. The output stock valuation of the above data is:
Item Qty Value
ABC 50 40000
XYZ 110 40500
LPQ 100 23700
Please help me to get the solution in FIFO
I got what I expected ,
The first observation is that we don't need to do anything special for OUT transactions - we just need to know the total quantity. That's what the TotalOut CTE calculates. The first two CTEs work with IN transactions, and compute what "interval" of stock each represents - change the final query to just select * from RunningTotals to get a feel for that.
If you have separate columns for date and time ,Then you should group by date first then time.