I have a table that stores Inventory Data by InventoryID and week. I need to somehow query and keep a running total of the DeltaQty from the prior weeks in order to figure out what is the Cumulative AvailQty for each week (Running total + AvailableQty)
CREATE TABLE InventoryWeekly (
StartDate date NULL,
EndDate date NULL,
InventoryID varchar(11) NULL,
AvailableQty float NULL,
OutgoingQty float NULL,
DeltaQty int NULL,
CumulativeAvailQty int NULL
);
INSERT INTO InventoryWeekly (InventoryID, StartDate, EndDate, OutgoingQty, AvailableQty, DeltaQty, CumulativeAvailQty)
VALUES
('00069','2023-01-09','2023-01-15', 1, 2, 1, 0),
('00069','2023-01-16','2023-01-22', 2, 2, 0, 0),
('00069','2023-01-23','2023-01-29', 3, 0, -3, 0),
('00071','2023-01-09','2023-01-15', 5, 8, 3, 0),
('00071','2023-01-16','2023-01-22', 2, 3, 1, 0),
('00071','2023-01-23','2023-01-29', 3, 1, -2, 0);
I've created this fiddle.
I've tried using LAG
function but it isn't cumulative.