SQL Window Functions: Running Totals, Moving Averages, and Cumulative Metrics/Centered moving average
Use prior and following rows together.
Section: Moving windows
Centered moving average
sql
sql
SELECT day,
value,
AVG(value) OVER (
ORDER BY day
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
) AS centered_avg_3
FROM metrics;Explanation
A centered frame is useful for smoothing but depends on future rows being present.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Moving windows