SQL · Module 9 · Lesson 41 min read

Running Totals, Moving Averages & Frames

Running total

Add ORDER BY inside OVER:

SQL
sum(amount) OVER (PARTITION BY user_id ORDER BY spent_on) AS running_total

Each row's value = the sum of all rows up to and including it.

The window frame

The frame decides which rows in the partition are included for each row.

SQL
ROWS BETWEEN <start> AND <end>

Start/end options:

  • UNBOUNDED PRECEDING – the first row of the partition
  • n PRECEDING – n rows before
  • CURRENT ROW
  • n FOLLOWING – n rows after
  • UNBOUNDED FOLLOWING – the last row of the partition

Defaults

  • With ORDER BY and no frame: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  • Without ORDER BY: the whole partition

ROWS vs RANGE vs GROUPS

  • ROWS – counts physical rows
  • RANGE – uses values: rows with the same ORDER BY value (ties/peers) are included together. RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW → a real time window
  • GROUPS – counts groups of tied rows

⚠️ With the default RANGE frame, two expenses on the same date get the same running total (both included). Use ROWS (or add a unique tie-breaker) for row-by-row totals.

Moving averages

  • Last 3 rows: avg(amount) OVER (ORDER BY spent_on ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
  • Last 7 days (by date): RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW

Common uses

  • Running account balance (deposits – withdrawals)
  • Cumulative spending vs budget (budget burn-down)
  • Smoothing noisy daily data with moving averages
  • Year-to-date (YTD) totals: PARTITION BY year ORDER BY month

Example

SQL
-- Running total for one user, row by row
SELECT spent_on, title, amount,
       sum(amount) OVER (ORDER BY spent_on, id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM expenses
WHERE user_id = 2
ORDER BY spent_on, id;
-- 2026-09-05 Lakeside cafe 650 → 650
-- 2026-09-18 Fuel 3000         → 3650 ... → 22630 at the end

-- Budget burn-down: November spending vs budget for each user
SELECT u.name, e.spent_on, e.amount,
       sum(e.amount) OVER (PARTITION BY u.id ORDER BY e.spent_on, e.id) AS spent_so_far,
       u.monthly_budget - sum(e.amount) OVER (PARTITION BY u.id ORDER BY e.spent_on, e.id) AS remaining
FROM expenses e JOIN users u ON u.id = e.user_id
WHERE e.spent_on >= '2026-11-01'
ORDER BY u.name, e.spent_on;

-- Moving average of the last 3 expenses
SELECT spent_on, amount,
       round(avg(amount) OVER (ORDER BY spent_on, id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 2) AS moving_avg_3
FROM expenses
WHERE user_id = 1
ORDER BY spent_on;

-- Time-based window: total spent in the 7 days up to each expense (all users)
SELECT spent_on, title, amount,
       sum(amount) OVER (ORDER BY spent_on RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW) AS last_7_days
FROM expenses
WHERE spent_on BETWEEN '2026-11-01' AND '2026-11-10'
ORDER BY spent_on;

-- ROWS vs default RANGE on tied dates
SELECT spent_on, amount,
       sum(amount) OVER (ORDER BY spent_on)                                   AS range_default,
       sum(amount) OVER (ORDER BY spent_on ROWS UNBOUNDED PRECEDING)          AS rows_frame
FROM (VALUES (DATE '2026-11-01', 100), ('2026-11-02', 50), ('2026-11-02', 70)) t(spent_on, amount);
-- range_default: 100, 220, 220   rows_frame: 100, 150, 220