clear markdown compare help best fiddles feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3601568 fiddles created (47975 in the last week).

CREATE TABLE sales([date] date, [sales] int, [rest] int); INSERT INTO sales VALUES ('20170101', 52, 0), ('20170102', 30, 0), ('20170103', 10, 200);
3 rows affected
 hidden batch(es)


WITH RN AS ( SELECT [date], [sales], [rest], COALESCE(LAG(sales) OVER (ORDER BY [date] DESC), 0) LG FROM sales ) SELECT [date], sales, SUM(LG + rest) OVER (ORDER BY [date] DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) rest FROM RN ORDER BY [date]
date sales rest
01/01/2017 00:00:00 52 240
02/01/2017 00:00:00 30 210
03/01/2017 00:00:00 10 200
 hidden batch(es)