SQL how-to
How to calculate a running total in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
SUM(value) OVER (ORDER BY date) gives a running total. Add PARTITION BY to restart it for each group, and a unique tie-breaker in ORDER BY so rows that share a date don't jump together.
The task: Add up a value row by row, so each row shows the total so far.
Orders per day, and the running total
A window function over a grouped query: each day's count, plus the sum of all counts up to that day.
SELECT ordered_on,
COUNT(*) AS orders,
SUM(COUNT(*)) OVER (ORDER BY ordered_on) AS running_orders
FROM orders
GROUP BY ordered_on
ORDER BY ordered_on;Result (PostgreSQL and SQLite, identical): first 12 of 14 rows
| ordered_on | orders | running_orders |
|---|---|---|
| 2026-03-01 | 2 | 2 |
| 2026-03-03 | 1 | 3 |
| 2026-03-04 | 1 | 4 |
| 2026-03-05 | 2 | 6 |
| 2026-03-07 | 1 | 7 |
| 2026-03-08 | 1 | 8 |
| 2026-03-10 | 1 | 9 |
| 2026-03-11 | 1 | 10 |
| 2026-03-12 | 1 | 11 |
| 2026-03-14 | 1 | 12 |
| 2026-03-15 | 1 | 13 |
| 2026-03-18 | 1 | 14 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
SUM(COUNT(*))The window sums each day's count; the inner COUNT(*) is the GROUP BY's aggregate.
- 2
OVER (ORDER BY ordered_on)Sum the rows from the first day up to the current one.
More examples
Why a tie-breaker matters
Orders 1 and 2 share a date. Ordered by date alone, both rows get the total up to the end of that day (2). With the id as a tie-breaker, the total climbs one row at a time.
SELECT id, ordered_on,
COUNT(*) OVER (ORDER BY ordered_on) AS by_date_only,
COUNT(*) OVER (ORDER BY ordered_on, id) AS with_tiebreaker
FROM orders
ORDER BY ordered_on, id
LIMIT 6;Result (PostgreSQL and SQLite, identical): 6 rows
| id | ordered_on | by_date_only | with_tiebreaker |
|---|---|---|---|
| 1 | 2026-03-01 | 2 | 1 |
| 2 | 2026-03-01 | 2 | 2 |
| 3 | 2026-03-03 | 3 | 3 |
| 4 | 2026-03-04 | 4 | 4 |
| 5 | 2026-03-05 | 6 | 5 |
| 6 | 2026-03-05 | 6 | 6 |
Try it: PostgreSQL playground · SQLite playground
A running total per group
Minutes listened so far, separately for each of two Streamly users.
SELECT user_id, played_on, minutes,
SUM(minutes) OVER (PARTITION BY user_id
ORDER BY played_on, id) AS minutes_so_far
FROM plays
WHERE user_id IN (1, 2)
ORDER BY user_id, played_on, id;Result (PostgreSQL and SQLite, identical): 1 row
| user_id | played_on | minutes | minutes_so_far |
|---|---|---|---|
| 2 | 2026-01-07 | 62 | 62 |
Try it: PostgreSQL playground · SQLite playground
Variations
A moving total instead of a running one
Limit the frame: SUM(x) OVER (ORDER BY d ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) adds the last seven rows only.
Running total as a share
Divide by the grand total, SUM(x) OVER (), for a cumulative percentage.
Common mistakes
No tie-breaker: rows with the same ORDER BY value get the same total, so the column jumps.
Expecting
ORDER BYat the end of the query to drive the window: the order insideOVER (…)is what counts.Using a self-join to add up earlier rows: correct but slow; the window function does it in one pass.
PostgreSQL and SQLite
Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.
Official documentation
Go deeper
- Guide: SQL window functions
- Practice: SQL window functions practice: 8 exercises with answers
- Lesson: Running totals (Pro)
Make it stick in five minutes a day
“Running totals” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.