HardPro challengeSQL
Monthly Running Total
SQLDatabases
Write a SQL query that returns each month alongside its sales amount and
the cumulative (running) total up to that month, ordered chronologically.
Table: `sales`
| id | month | amount |
|---|---|---|
| 1 | 2024-01 | 100 |
| 2 | 2024-02 | 150 |
| 3 | 2024-03 | 200 |
| 4 | 2024-04 | 120 |
Expected output (columns: month, amount, running_total)
2024-01|100|100
2024-02|150|250
2024-03|200|450
2024-04|120|570Hint — use
SUM(amount) OVER (ORDER BY month)as a window function.
Sample tests
Test #14 months — running total reaches 570
Input: "CREATE TABLE sales (id INTEGER, month TEXT, amount INTEGER);\nINSERT INTO sales VALUES\n (1, '2024-01', 100),\n (2, '2024-02', 150),\n (3, '2024-03', 200),\n (4, '2024-04', 120);"
Output: "2024-01|100|100\n2024-02|150|250\n2024-03|200|450\n2024-04|120|570"