Finance publishes a cumulative revenue line for each product line across 2026.
From monthly_revenue, return product_line, revenue_month, revenue and running_total — the cumulative revenue for that product line from the earliest month up to and including the current month.
Sample data
Table: monthly_revenue
16 row(s) · showing first 8
| row_idINT | product_lineTEXT | revenue_monthDATE | revenueNUMERIC(12,2) |
|---|---|---|---|
| 1 | Payments | 2026-01-01 | 420000.00 |
| 2 | Payments | 2026-02-01 | 388000.00 |
| 3 | Payments | 2026-03-01 | 465000.00 |
| 4 | Payments | 2026-04-01 | 502000.00 |
| 5 | Lending | 2026-01-01 | 180000.00 |
| 6 | Lending | 2026-02-01 | 225000.00 |
| 7 | Lending | 2026-03-01 | 198000.00 |
| 8 | Lending | 2026-04-01 | 260000.00 |
monthly_revenue
- row_idINT
- product_lineTEXT
- revenue_monthDATE
- revenueNUMERIC(12,2)
- Step 1: A cumulative measure keeps every input row, so use `SUM()` as a window function instead of a `GROUP BY`.
- Step 2: `PARTITION BY product_line` restarts the accumulation for each product line.
- Step 3: `ORDER BY revenue_month` defines the direction the total accumulates in.
- Step 4: `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW` states the frame explicitly — everything from the first month through the current one.
- Step 5: Alias it `running_total` and keep the original `revenue` beside it so the month-on-month contribution stays visible.
SELECT product_line,
revenue_month,
revenue,
SUM(revenue) OVER (
PARTITION BY product_line
ORDER BY revenue_month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM monthly_revenue;Learn the concept first
Stuck on Window Functions? These free SQL lessons teach it step by step.
More Window Functions questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program