sqlstreak

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_onplaysavg_3d
2026-02-012121
2026-02-021719
2026-02-031618
2026-02-042419
2026-02-051719
2026-02-061619
2026-02-071716.7
2026-02-082318.7
2026-02-092421.3
2026-02-101821.7
2026-02-112020.7
2026-02-122521

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    COUNT(*) … GROUP BY played_on

    First one row per day.

  2. 2
    AVG(COUNT(*)) OVER (ORDER BY played_on …)

    Then average those daily counts in date order.

  3. 3
    ROWS BETWEEN 2 PRECEDING AND CURRENT ROW

    The 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_onplaysavg_centred
2026-02-012119
2026-02-021718
2026-02-031619
2026-02-042419
2026-02-051719
2026-02-061616.7
2026-02-071718.7
2026-02-082321.3
2026-02-092421.7
2026-02-101820.7
2026-02-112021
2026-02-122521.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

speciesnamearrived_onweight_kgavg_with_previous
catTofu2025-04-044.64.6
catSocks2025-08-177.46
catPumpkin2025-10-315.26.3
catMochi2025-11-024.14.65
catLuna2026-02-033.83.95
catOlive2026-03-223.93.85
catPepper2026-04-193.23.55
rabbitJuniper2025-12-303.43.4
rabbitClover2026-02-272.12.75
rabbitWaffles2026-03-111.61.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

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.

Start free