# How to calculate a moving average in SQL

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


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

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

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

(8 of 14 rows)

### Centred on the day

One day either side instead of two days before.

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

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

(8 of 14 rows)

### A moving average per group

Each pet's weight averaged with the previous pet to arrive of the same species.

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

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

(8 of 10 rows)

## How it works

- `COUNT(*) … GROUP BY played_on`: First one row per day.
- `AVG(COUNT(*)) OVER (ORDER BY played_on …)`: Then average those daily counts in date order.
- `ROWS BETWEEN 2 PRECEDING AND CURRENT ROW`: The frame: this row and the two rows before it. The first rows simply average fewer days.

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

Same in both engines.
