Question 1 · choose 1
A Redshift table daily_sales has one row per store per day (store_id, sale_date, revenue), with no missing days. Analysts need each store's 7-day rolling average revenue, including the current day, next to every row. Which expression should they use?
- AAVG(revenue) OVER (PARTITION BY store_id ORDER BY sale_date ROWS BETWEEN 7 PRECEDING AND CURRENT ROW)
- BAVG(revenue) OVER (PARTITION BY store_id ORDER BY sale_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)
- CAVG(revenue) OVER (PARTITION BY store_id), with no ORDER BY in the window
- DAVG(revenue) with GROUP BY store_id, sale_date in the same query
Show the answer and why
AAVG(revenue) OVER (PARTITION BY store_id ORDER BY sale_date ROWS BETWEEN 7 PRECEDING AND CURRENT ROW)
Incorrect
Seven preceding rows plus the current row is an 8-day window.
BAVG(revenue) OVER (PARTITION BY store_id ORDER BY sale_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)
Correct
The frame covers the current row and the 6 rows before it in date order, which is 7 days for each store, and a window function keeps every row.
CAVG(revenue) OVER (PARTITION BY store_id), with no ORDER BY in the window
Incorrect
Without ORDER BY the frame is the whole partition, so every row gets the store's all-time average.
DAVG(revenue) with GROUP BY store_id, sale_date in the same query
Incorrect
Grouping by store and day averages one row per group. It returns each day's own revenue, not a rolling value.
Rolling metrics are window functions with an ordered frame. ROWS BETWEEN n PRECEDING AND CURRENT ROW covers n + 1 rows.
AWS documentation