How to Calculate Cumulative Sum-Running Total

Let’s say we want to see a report with cumulative values, for example the cumulative daily revenue at different timestamps. We want the cumulative revenue at each timestamp in the table:

sales_ts amount
2018-01-01 00:10:10 120
2018-01-01 00:11:00 100
2018-01-01 00:11:20 100
2018-01-01 00:12:12 90
2018-01-01 00:12:00 220
2018-01-02 00:11:20 80
2018-01-02 00:11:32 190
2018-01-02 00:12:00 220
select sales_ts,
sum(amount) over (partition by sales_ts order by sales_ts) 
from sales;

The previous query calculates the cumulative revenue considering only the records with timestamps previous to the current timestamp.

sales_ts amount
2018-01-01 00:10:10 120
2018-01-01 00:11:00 220
2018-01-01 00:11:20 320
2018-01-01 00:12:00 540
2018-01-01 00:12:12 630
2018-01-02 00:11:20 80
2018-01-02 00:11:32 270
2018-01-02 00:12:00 490

IN THIS PAGE