Example 1
Show the cumulative revenue total for each day
Each row's running_total is the sum of all amounts from the first row through the current row in date order. Jan 1 starts at 100; Jan 2 adds 150 to reach 250; Jan 3 adds 80 to reach 330; Jan 4 adds 200 to reach 530. Add PARTITION BY category before ORDER BY to reset the running total for each category.
CREATE TABLE sales (id INT, sale_date VARCHAR(10), amount INT);
INSERT INTO
sales (id, sale_date, amount)
VALUES
(1, '2024-01-01', 100),
(2, '2024-01-02', 150),
(3, '2024-01-03', 80),
(4, '2024-01-04', 200);SELECT
sale_date,
amount,
SUM(amount) OVER (
ORDER BY
sale_date
) AS running_total
FROM
sales
ORDER BY
sale_date;| sale_date | amount | running_total |
|---|---|---|
| 2024-01-01 | 100 | 100 |
| 2024-01-02 | 150 | 250 |
| 2024-01-03 | 80 | 330 |
| 2024-01-04 | 200 | 530 |
Output is identical across all supported engine versions.