SQL how-to
How to calculate a moving average in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
AVG(value) OVER (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) averages each row with the two before it: a 3-day moving average. Change the numbers for a wider window, or use 1 PRECEDING AND 1 FOLLOWING to centre it.
The task: Smooth a daily series by averaging each day with the days around it.
Plays per day, with a 3-day moving average
SELECT played_on, COUNT(*) AS plays,
ROUND(AVG(COUNT(*)) OVER (ORDER BY played_on
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS avg_3d
FROM plays
WHERE played_on BETWEEN '2026-02-01' AND '2026-02-14'
GROUP BY played_on
ORDER BY played_on;Result (PostgreSQL and SQLite, identical): first 12 of 14 rows
| played_on | plays | avg_3d |
|---|---|---|
| 2026-02-01 | 21 | 21 |
| 2026-02-02 | 17 | 19 |
| 2026-02-03 | 16 | 18 |
| 2026-02-04 | 24 | 19 |
| 2026-02-05 | 17 | 19 |
| 2026-02-06 | 16 | 19 |
| 2026-02-07 | 17 | 16.7 |
| 2026-02-08 | 23 | 18.7 |
| 2026-02-09 | 24 | 21.3 |
| 2026-02-10 | 18 | 21.7 |
| 2026-02-11 | 20 | 20.7 |
| 2026-02-12 | 25 | 21 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
COUNT(*) … GROUP BY played_onFirst one row per day.
- 2
AVG(COUNT(*)) OVER (ORDER BY played_on …)Then average those daily counts in date order.
- 3
ROWS BETWEEN 2 PRECEDING AND CURRENT ROWThe frame: this row and the two rows before it. The first rows simply average fewer days.
More examples
Centred on the day
One day either side instead of two days before.
SELECT played_on, COUNT(*) AS plays,
ROUND(AVG(COUNT(*)) OVER (ORDER BY played_on
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING), 1) AS avg_centred
FROM plays
WHERE played_on BETWEEN '2026-02-01' AND '2026-02-14'
GROUP BY played_on
ORDER BY played_on;Result (PostgreSQL and SQLite, identical): first 12 of 14 rows
| played_on | plays | avg_centred |
|---|---|---|
| 2026-02-01 | 21 | 19 |
| 2026-02-02 | 17 | 18 |
| 2026-02-03 | 16 | 19 |
| 2026-02-04 | 24 | 19 |
| 2026-02-05 | 17 | 19 |
| 2026-02-06 | 16 | 16.7 |
| 2026-02-07 | 17 | 18.7 |
| 2026-02-08 | 23 | 21.3 |
| 2026-02-09 | 24 | 21.7 |
| 2026-02-10 | 18 | 20.7 |
| 2026-02-11 | 20 | 21 |
| 2026-02-12 | 25 | 21.7 |
Try it: PostgreSQL playground · SQLite playground
A moving average per group
Each pet's weight averaged with the previous pet to arrive of the same species.
SELECT species, name, arrived_on, weight_kg,
ROUND(AVG(weight_kg) OVER (PARTITION BY species ORDER BY arrived_on
ROWS BETWEEN 1 PRECEDING AND CURRENT ROW), 2) AS avg_with_previous
FROM pets
WHERE species IN ('cat', 'rabbit')
ORDER BY species, arrived_on;Result (PostgreSQL and SQLite, identical): 10 rows
| species | name | arrived_on | weight_kg | avg_with_previous |
|---|---|---|---|---|
| cat | Tofu | 2025-04-04 | 4.6 | 4.6 |
| cat | Socks | 2025-08-17 | 7.4 | 6 |
| cat | Pumpkin | 2025-10-31 | 5.2 | 6.3 |
| cat | Mochi | 2025-11-02 | 4.1 | 4.65 |
| cat | Luna | 2026-02-03 | 3.8 | 3.95 |
| cat | Olive | 2026-03-22 | 3.9 | 3.85 |
| cat | Pepper | 2026-04-19 | 3.2 | 3.55 |
| rabbit | Juniper | 2025-12-30 | 3.4 | 3.4 |
| rabbit | Clover | 2026-02-27 | 2.1 | 2.75 |
| rabbit | Waffles | 2026-03-11 | 1.6 | 1.85 |
Try it: PostgreSQL playground · SQLite playground
Variations
Missing days
ROWS counts rows, not calendar days. If some days have no data, generate the calendar first and LEFT JOIN, so a gap counts as 0.
A rolling sum
Same frame with SUM: SUM(x) OVER (ORDER BY d ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) is a 7-day total.
Common mistakes
Leaving out the frame: with ORDER BY, the default frame runs from the first row, so you get a running average, not a moving one.
RANGE instead of ROWS with repeated dates: rows with the same date are averaged together.
Rounding inside the window instead of around it.
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 frames, moving averages, NTILE and ROLLUP
- Lesson: Frames and shares (Pro)
Make it stick in five minutes a day
“Frames and shares” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.