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. 3601446 fiddles created (47986 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
2017-01-01 52 240
2017-01-02 30 210
2017-01-03 10 200
 hidden batch(es)