Back to problems

Compute a Seven-Day Rolling Average

SQL · Whatnot · Medium

Given a table daily_metrics(metric_date DATE, metric_value NUMERIC), write a single PostgreSQL SELECT or CTE query that computes a seven-day trailing average for each row. Do not create, alter, or modify any tables. For a row whose date is d, the trailing average is the average of all non-NULL metric_value rows whose metric_date is in the calendar range $$d-6 \le \text{metric_date} \le d$$. The window is based strictly on calendar dates: if a date is not present in the…

Checking your access…