# How to calculate a running total in SQL

Source: https://sqlstreak.com/sql/how-to/running-total · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


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

```sql
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 (both engines):

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

(8 of 14 rows)

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

```sql
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 (both engines):

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

### A running total per group

Minutes listened so far, separately for each of two Streamly users.

```sql
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 (both engines):

| user_id | played_on | minutes | minutes_so_far |
| --- | --- | --- | --- |
| 2 | 2026-01-07 | 62 | 62 |

## How it works

- `SUM(COUNT(*))`: The window sums each day's count; the inner COUNT(*) is the GROUP BY's aggregate.
- `OVER (ORDER BY ordered_on)`: Sum the rows from the first day up to the current one.

## 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

Same in both engines.
