I'm putting a link to this blog of Kim Berg Hansen to have it for my personal reference.
http://www.kibeha.dk/2011/07/analytic-fifo-picking.html
SELECT ordno,
item,
ord_qty,
loc,
loc_qty,
purch,
LEAST (loc_qty, ord_qty - NVL (picked_qty, 0)) pick_qty
FROM (SELECT ol.ordno,
ol.item,
ol.qty ord_qty,
i.loc,
i.qty loc_qty,
i.purch,
SUM (
i.qty)
OVER (PARTITION BY i.item
ORDER BY purch, loc
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
picked_qty
FROM inventory i, orderline ol
WHERE i.item = ol.item)
WHERE NVL (picked_qty, 0) < ord_qty;
ORDNO ITEM ORD_QTY LOC LOC_QTY PURCH PICK_QTY
---------- ---------- ---------- ---------- ---------- --------- ----------
1 A1 24 1-A-20 18 01-NOV-04 18
1 A1 24 2-A-02 24 02-NOV-04 6
1 B1 18 1-B-15 2 02-NOV-04 2
1 B1 18 1-C-04 12 03-NOV-04 12
1 B1 18 2-D-23 1 04-NOV-04 1
1 B1 18 1-B-11 4 05-NOV-04 3
6 rows selected.
http://www.kibeha.dk/2011/07/analytic-fifo-picking.html
SELECT ordno,
item,
ord_qty,
loc,
loc_qty,
purch,
LEAST (loc_qty, ord_qty - NVL (picked_qty, 0)) pick_qty
FROM (SELECT ol.ordno,
ol.item,
ol.qty ord_qty,
i.loc,
i.qty loc_qty,
i.purch,
SUM (
i.qty)
OVER (PARTITION BY i.item
ORDER BY purch, loc
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
picked_qty
FROM inventory i, orderline ol
WHERE i.item = ol.item)
WHERE NVL (picked_qty, 0) < ord_qty;
ORDNO ITEM ORD_QTY LOC LOC_QTY PURCH PICK_QTY
---------- ---------- ---------- ---------- ---------- --------- ----------
1 A1 24 1-A-20 18 01-NOV-04 18
1 A1 24 2-A-02 24 02-NOV-04 6
1 B1 18 1-B-15 2 02-NOV-04 2
1 B1 18 1-C-04 12 03-NOV-04 12
1 B1 18 2-D-23 1 04-NOV-04 1
1 B1 18 1-B-11 4 05-NOV-04 3
6 rows selected.