← Back to SQL, page 6

sql-0117 · Medium mid

Write a query for a 3-day moving average of sales.

Use AVG(amount) OVER (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW). The first two rows average over fewer days because there are not enough earlier rows. ROWS counts rows, not calendar days, so if days are missing from the table, fill the calendar first or the window will span more than three days.

Full answer
sql
CREATE TABLE daily_sales (
  day    TEXT PRIMARY KEY,
  amount INTEGER NOT NULL
);
INSERT INTO daily_sales VALUES
  ('2026-03-01',120),('2026-03-02',80),('2026-03-03',150),
  ('2026-03-05',90),('2026-03-06',60),('2026-03-09',200);
sql
SELECT day, amount,
       ROUND(AVG(amount) OVER (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS avg_3_rows
FROM daily_sales
ORDER BY day;
dayamountavg_3_rows
2026-03-01120120.0
2026-03-0280100.0
2026-03-03150116.7
2026-03-0590106.7
2026-03-0660100.0
2026-03-09200116.7

The frame ROWS BETWEEN 2 PRECEDING AND CURRENT ROW means "this row and the two before it". On 1 March only one row exists, so the average is the amount itself; on 2 March it averages two rows.

There is a catch in this data. The table has no rows for 4, 7 and 8 March, so the "3-day" average on 9 March actually covers 5, 6 and 9 March. ROWS counts rows, whatever dates they hold.

If the business means three calendar days, generate the missing days first and treat them as zero:

sql
WITH RECURSIVE days(day) AS (
  SELECT '2026-03-01' UNION ALL
  SELECT date(day, '+1 day') FROM days WHERE day < '2026-03-09'
)
SELECT d.day, COALESCE(s.amount, 0) AS amount,
       ROUND(AVG(COALESCE(s.amount, 0)) OVER (ORDER BY d.day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS avg_3_days
FROM days d LEFT JOIN daily_sales s ON s.day = d.day
ORDER BY d.day;
dayamountavg_3_days
2026-03-01120120.0
2026-03-0280100.0
2026-03-03150116.7
2026-03-04076.7
2026-03-059080.0
2026-03-066050.0
2026-03-07050.0
2026-03-08020.0
2026-03-0920066.7

Now the 9 March value covers 7, 8 and 9 March. To hide the incomplete averages at the start, add a count in the same frame and blank the value when it is below 3:

text
CASE WHEN COUNT(*) OVER w = 3 THEN AVG(amount) OVER w END
sql
CREATE TABLE daily_sales (
  day    TEXT PRIMARY KEY,
  amount INTEGER NOT NULL
);
INSERT INTO daily_sales VALUES
  ('2026-03-01',120),('2026-03-02',80),('2026-03-03',150),
  ('2026-03-05',90),('2026-03-06',60),('2026-03-09',200);

SELECT day, amount,
       ROUND(AVG(amount) OVER (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS avg_3_rows
FROM daily_sales
ORDER BY day;

WITH RECURSIVE days(day) AS (
  SELECT '2026-03-01' UNION ALL
  SELECT date(day, '+1 day') FROM days WHERE day < '2026-03-09'
)
SELECT d.day, COALESCE(s.amount, 0) AS amount,
       ROUND(AVG(COALESCE(s.amount, 0)) OVER (ORDER BY d.day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 1) AS avg_3_days
FROM days d LEFT JOIN daily_sales s ON s.day = d.day
ORDER BY d.day;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • ROWS BETWEEN 2 PRECEDING AND CURRENT ROW is a 3-row window
  • Early rows have smaller windows
  • ROWS counts rows, not calendar days
  • Fill missing days first when the window must be calendar-based

Common mistakes

  • Calling a 3-row window a 3-day window when days are missing
  • Using the default frame, which is a running average, not a moving one

Follow-ups

  • How would you compute a 7-day moving sum per store?
  • Could RANGE with an interval solve the missing-day problem in PostgreSQL?

Tips

  • Point out the gap in the data before showing the fix