Problem Statement
Explain how to compute a 7-day moving average per product. Mention the window frame choice and why.
Explanation
Partition by product so each series is independent. Order by date so rows stream in time order. Use a ROWS frame of six preceding to current row to capture exactly seven rows, which is stable even with multiple rows per day.
RANGE frames can include peers with the same date and change the number of rows. ROWS keeps the window size fixed and matches the business definition precisely.
Code Solution
SolutionRead Only
AVG(amount) OVER ( PARTITION BY product_id ORDER BY sale_date, id ROWS BETWEEN 6 PRECEDING AND CURRENT ROW ) AS avg_7d
