IntermediateWindow functions

Running total of revenue

Task

sales has one row per sale. Return one row per day with that day's revenue and the running total up to and including that day, ordered by day. Columns: day, revenue, running_total.

Order matters: sort your rows as asked. Column names do not have to match. SQLite dialect.

Tables

sales

day TEXT, amount INTEGER

dayamount
2026-03-01100
2026-03-0150
2026-03-0280
2026-03-03120
2026-03-0330
2026-03-0440
Expected output
dayrevenuerunning_total
2026-03-01150150
2026-03-0280230
2026-03-03150380
2026-03-0440420
Show solution
SQL
SELECT day,
       SUM(amount) AS revenue,
       SUM(SUM(amount)) OVER (ORDER BY day) AS running_total
FROM sales
GROUP BY day
ORDER BY day;

Window functions run after GROUP BY, so SUM(SUM(amount)) OVER (ORDER BY day) adds up the daily totals. Without grouping first you get one row per sale, and tied days share the same running total.

The full explanation, with more examples, is in the question Running total of revenue.