sql >> Base de Datos >  >> RDS >> Sqlserver

sql actualiza la columna int basada en la columna int anterior

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;