The row-count vs calendar-day distinction is what makes this non-trivial.
First, generate a complete date series to fill missing dates, then left join the daily metrics and replace nulls with zeros. Use a window function with RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW to compute the rolling average based on calendar days, ensuring the window covers exactly 7 days.
Pro tip: Mention that using ROWS would incorrectly count rows, so RANGE is essential for calendar-day windows. Also, clarify how to handle missing dates (e.g., zero-fill) and note that the average should be over 7 days, not just existing days.
Create a date spine covering the entire period of interest to ensure no calendar days are missing. This can be done with a recursive CTE or a calendar table.
Left join the date spine to the daily metrics table and replace null DAU values with 0 (or leave as null if appropriate). This ensures every date has a value for the rolling calculation.
Use AVG(dau) OVER (ORDER BY date RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW) to calculate the 7-day rolling average based on calendar days, not row count.
Consider how to handle the first few days where the window is incomplete (e.g., require full 7 days or allow partial). Validate results by checking a few dates manually.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.