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 w, 1) AS centered_avg,
COUNT(*) OVER w AS rows_in_frame
FROM daily_sales
WINDOW w AS (ORDER BY day ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
ORDER BY day;| day | amount | centered_avg | rows_in_frame |
|---|---|---|---|
| 2026-03-01 | 120 | 100.0 | 2 |
| 2026-03-02 | 80 | 116.7 | 3 |
| 2026-03-03 | 150 | 106.7 | 3 |
| 2026-03-05 | 90 | 100.0 | 3 |
| 2026-03-06 | 60 | 116.7 | 3 |
| 2026-03-09 | 200 | 130.0 | 2 |
The frame reaches one row back and one row forward. The first row has no predecessor and the last no successor, so their windows hold two rows. Counting rows in the same frame shows that directly.
Centered averages smooth a series without shifting peaks in time, which suits charts. They cannot be computed for the latest day until the next day's data arrives, so live dashboards usually prefer trailing windows.
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 w, 1) AS centered_avg,
COUNT(*) OVER w AS rows_in_frame
FROM daily_sales
WINDOW w AS (ORDER BY day ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
ORDER BY day;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING centers the window
- Edge rows have smaller windows
- Centered windows need future data
- ROWS counts rows, not days
Common mistakes
- Using a centered window on live data and seeing the last value change
- Forgetting that missing days distort row-based windows
Follow-ups
- How would you hide averages with fewer than 3 values?
- When is a trailing window better?
Tips
- Show the frame size per row