Section: Moving windows

3-row moving average

sql
sql
SELECT day,
       revenue,
       AVG(revenue) OVER (
         ORDER BY day
         ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
       ) AS moving_avg_3
FROM daily_revenue;
Explanation

A row-based moving average is stable when each row is one time bucket.

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
7-row moving sum
Rolling weekly sum on daily rows.
OpenIn sheetsqlsame section
Centered moving average
Use prior and following rows together.
OpenIn sheetsqlsame section
Running total per category
Reset the running total by partition.
OpenIn sheetsqlsame section
Running total
Cumulative sum ordered by date.
Running count
Count rows seen so far.
Cumulative maximum
Track the best-so-far value.