Nastavení schématu Oracle 11g R2 :
CREATE TABLE lots ( Lot_Size, Reported_QTY ) AS
SELECT 150, 100 FROM DUAL
UNION ALL SELECT 150, 100 FROM DUAL
UNION ALL SELECT 150, 80 FROM DUAL
UNION ALL SELECT 150, 80 FROM DUAL
UNION ALL SELECT 150, 5 FROM DUAL;
Dotaz 1 :
SELECT Lot_Size,
Reported_QTY,
COALESCE( LAG( Reported_QTY ) OVER ( ORDER BY NULL ) - Reported_QTY,
Lot_Size - Reported_QTY ) AS Qty_Balance
FROM Lots
Výsledky :
| LOT_SIZE | REPORTED_QTY | QTY_BALANCE |
|----------|--------------|-------------|
| 150 | 100 | 50 |
| 150 | 100 | 0 |
| 150 | 80 | 20 |
| 150 | 80 | 0 |
| 150 | 5 | 75 |