Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

sql aktualisiert die int-Spalte basierend auf der vorherigen int-Spalte

CREATE TABLE #FruitSales
(
   SaleForDay    DECIMAL (10, 2),
   SalesToDate   DECIMAL (10, 2),
   ItemOrder     INT
);

INSERT INTO #FruitSales (SaleForDay, SalesToDate, ItemOrder)
VALUES ('4.99', '4.99', '1'),
       ('12.99', NULL, '2'),
       ('14.99', NULL, '3'),
       ('5.99', NULL, '4');

update f
SET SalesToDate = (select sum(saleforday) from #FruitSales ff where ff.itemorder <= f.itemorder)
from #FruitSales f

SELECT * FROM #FruitSales;