Publicis Sapient is analyzing daily revenue trends for an active-user reporting dashboard. Write a PostgreSQL query that calculates a rolling seven-day average of daily revenue for each active user from January 1 through January 7, 2024.
Requirements
- Aggregate multiple revenue events for the same user and calendar date into one daily revenue value.
- Include dates with no revenue as zero, so the rolling calculation represents calendar days rather than only transaction days.
- Use a window function partitioned by user and ordered by date. The window should include the current date and up to the six preceding calendar dates.
- Return active users, the date, daily revenue, and the rolling average, ordered by user and date.
Output details
- Return columns named
user_id, revenue_date, daily_revenue, and rolling_7_day_average, in that order.
- Sort by
user_id ascending, then revenue_date ascending.
- Round
rolling_7_day_average to exactly two decimal places.
- Treat NULL aggregated revenue as
0.