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;| day | amount | avg_3_rows |
|---|---|---|
| 2026-03-01 | 120 | 120.0 |
| 2026-03-02 | 80 | 100.0 |
| 2026-03-03 | 150 | 116.7 |
| 2026-03-05 | 90 | 106.7 |
| 2026-03-06 | 60 | 100.0 |
| 2026-03-09 | 200 | 116.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;| day | amount | avg_3_days |
|---|---|---|
| 2026-03-01 | 120 | 120.0 |
| 2026-03-02 | 80 | 100.0 |
| 2026-03-03 | 150 | 116.7 |
| 2026-03-04 | 0 | 76.7 |
| 2026-03-05 | 90 | 80.0 |
| 2026-03-06 | 60 | 50.0 |
| 2026-03-07 | 0 | 50.0 |
| 2026-03-08 | 0 | 20.0 |
| 2026-03-09 | 200 | 66.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 ENDsql
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