Sample Header Ad - 728x90

Calculate Quantity Based on First in, First Out (FIFO)

6 votes
1 answer
9495 views
I'm trying to get result of quantity based on FIFO, with 2 tables below: Table Purchase: | PO | Date | Quantity | Item | |-------|--------------|----------|------| | PO001 | 01-Jan-2016 | 3 | AO21 | | PO002 | 10-Jan-2016 | 7 | AO21 | | PO003 | 01-Feb-2016 | 3 | AO21 | Table Stock: | SO | Date | Quantity | Item | |-------|-------------|----------|------| | SO001 | 02-Jan-2016 | 2 | AO21 | | SO002 | 11-Feb-2016 | 8 | AO21 | | SO003 | 12-Feb-2016 | 6 | AO23 | I want output to be like this : | SO | PO | Quantity | |-------|-------|----------| | SO001 | PO001 | 2 | | SO002 | PO001 | 1 | | SO002 | PO003 | 7 | Do you have any idea of query for view this output? Result from calculated SO and PO in row. some more explanation: > Where do the 2, 1, 7 come from in the wanted result? From stock and purchase. The first (by date) stock value for item A021 has 2 and the first purchase (PO001) needs 3, so stock sold 2 and we get this row in the result: | SO001 | PO001 | 2 | We still need 1 more for the purchase and then next stock value is 8. So this purchase is completed and we get 1 (and 7 left in stock): | SO002 | PO001 | 1 | The next purchase (PO002) needs 7 and we have exactly 7 left, so the purchase is completed (and 0 stock left for that item). We get: | SO002 | PO003 | 7 | Purchase PO003 needs 3 but there is no stock left, so we get no rows in the result for that purchase.
Asked by Mei_R (63 rep)
Nov 8, 2016, 01:14 PM
Last activity: Dec 6, 2023, 03:02 PM