SPROC アプローチが必要な場合は、こちらをご覧ください。これは、これまでのすべてのシナリオで機能しました。使用したデータは次のとおりです。
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(1,'001',1656)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(2,'001',1986)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(3,'001',-283)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(4,'001',2718)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(5,'001',-4019)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(6,'001',7212)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(7,'001',782)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(8,'001',2073)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(9,'001',1842)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(10,'001',-12159)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(1,'002',1980)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(2,'002',-306)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(3,'002',826)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(1,'003',2592)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(2,'003',2592)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(3,'003',2448)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(4,'003',-4836)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(5,'003',1968)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(6,'003',1074)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(7,'003',324)
--INSERT INTO TestQuantity(ID, SKU, Quantity) Values(8,'003',1638)
SQLは次のとおりです。
CREATE TABLE #Results(ID int, SKU varchar(50), Quantity int)
DECLARE @sku as VARCHAR(50)
DECLARE skuCursor CURSOR FOR
SELECT DISTINCT SKU FROM TestQuantity WHERE Quantity < 0
OPEN skuCursor
FETCH NEXT FROM skuCursor INTO @sku
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @id AS INT
DECLARE @quantity AS INT
DECLARE quantityCursor CURSOR FOR
SELECT ID, Quantity FROM TestQuantity WHERE SKU=@sku AND ID <= (SELECT MAX(ID) FROM TestQuantity WHERE SKU=@sku and Quantity < 0) ORDER BY ID DESC
OPEN quantityCursor
FETCH NEXT FROM quantityCursor
INTO @id, @quantity
DECLARE @firstID AS INT = @id
DECLARE @sum AS INT = -1
DECLARE @currentID AS INT
DECLARE @currentQuantity AS INT
WHILE @@FETCH_STATUS = 0
BEGIN
IF @sum < 0
BEGIN
SELECT @currentID = ID, @currentQuantity = Quantity FROM TestQuantity WHERE SKU=@sku AND ID=@id - 1
SET @sum = @currentQuantity + @quantity
WHILE(@sum < 0)
BEGIN
SELECT @currentID = ID, @currentQuantity = Quantity FROM TestQuantity WHERE SKU=@sku AND ID=@currentID - 1
SET @sum = @currentQuantity + @sum
END
END
FETCH NEXT FROM quantityCursor
INTO @id, @quantity
END
INSERT INTO #results(ID, SKU, Quantity)
SELECT ID, SKU, QUANTITY FROM TestQuantity WHERE ID > @firstID and SKU=@sku
INSERT INTO #results(ID, SKU, Quantity)
SELECT ID, SKU, QUANTITY FROM TestQuantity WHERE ID < @currentID and SKU=@sku
INSERT INTO #results(ID, SKU, Quantity)
SELECT @currentID, @sku, @sum
CLOSE quantityCursor
DEALLOCATE quantityCursor
FETCH NEXT FROM skuCursor INTO @sku
END
CLOSE skuCursor
DEALLOCATE skuCursor
SELECT * FROM #results order by sku, id
DROP TABLE #results
私の結果:
1 001 1656
2 001 152
1 002 1674
3 002 826
1 003 2592
2 003 204
5 003 1968
6 003 1074
7 003 324
8 003 1638