I am working on implementing FIFO in sql. I have a batch number concept in my application. Assuming that I am selling on inventory, then my application should tell me which inventory is the first. Let's. Say I bought inventory “A” on August 4th, August 5th and August 6th.
On 4th Aug - A Inventory has batch number BT002 - 10 (Qty) On 5th Aug - A Inventory has batch number BT003 - 15 (Qty) On 6th Aug - A Inventory has batch number BT001 - 10 (Qty)
So, now I have a stock. Now in my hand the following:
A Inventory BT002 - 10 - 4-Aug BT003 - 15 - 5-Aug BT001 - 10 - 6-Aug
Now, if I want to sell this inventory to someone, then my application should tell me that I must sell BT002 (lot number) First, before it appears first.
It was a concept that I use in my application.
Now I want to sell 15 Qty from "A" (Inventory).
Then O / p should be as follows:
BT002 - 10 BT003 - 5
Here is my request:
SELECT ISNULL(SUM(qty),0) AS Qty,batch_no,accept_date FROM RS_GIN_Master GROUP BY batch_no,accept_date HAVING ISNULL(SUM(qty),0) <= 15 ORDER BY accept_date asc
O / p of this request:

How can I get O / P as follows:
BT002 - 10 BT003 - 5
Any help would be greatly appreciated. Thank you in advance.
source share