sqlstreak

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_onordersrunning_orders
2026-03-0122
2026-03-0313
2026-03-0414
2026-03-0526
2026-03-0717
2026-03-0818
2026-03-1019
2026-03-11110
2026-03-12111
2026-03-14112
2026-03-15113
2026-03-18114

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    SUM(COUNT(*))

    The window sums each day's count; the inner COUNT(*) is the GROUP BY's aggregate.

  2. 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

idordered_onby_date_onlywith_tiebreaker
12026-03-0121
22026-03-0122
32026-03-0333
42026-03-0444
52026-03-0565
62026-03-0566

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_idplayed_onminutesminutes_so_far
22026-01-076262

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 BY at the end of the query to drive the window: the order inside OVER (…) 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

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.

Start free