How do you build a rolling time window such as a trailing 7- or 28-day average, and how does a time-based RANGE frame differ from a row-count ROWS frame here?
Last updated
A rolling average uses a window frame, e.g.
AVG(value) OVER (ORDER BY day ...), aggregating each row with a trailing span of neighbors.ROWS N PRECEDINGcounts physical rows, so gaps in daily data silently stretch the window further back in time.RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROWspans real calendar time (a true 7-day window) and groups tied order values together.
- https://datasciencehired.com/blog/calculate-moving-averages-sql/
- https://learnsql.com/blog/difference-between-rows-range-window-functions/
- https://algomaster.io/learn/sql/introduction-to-window-functions
- https://docs.snowflake.com/en/developer-guide/snowflake-ml/feature-store/examples
- https://www.youtube.com/watch?v=Vq9H8_wMn0k&vl=en
- https://alexanderobregon.substack.com/p/how-to-create-a-rolling-7-day-average
- https://theartofpostgresql.com/yesql/window-functions/frame-semantics/