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
select 	sales_ts,
        sum(amount) over (order by sales_ts) as cumulative
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

IN THIS PAGE