AARP.org wants to monitor engagement trends for active users. Write a PostgreSQL query that aggregates engagement by user and day, then calculates both cumulative engagement and a three-day moving average.
Requirements
- Join users to engagement events and include only users whose
account_status is active.
- Aggregate engagement minutes by user and
engagement_date for January 1 through January 5, 2025. Treat a day containing only NULL minutes as zero.
- Calculate each user's running total using a window function ordered by date.
- Calculate a three-row moving average of daily engagement, including the current day and up to the two preceding observed engagement dates. Round it to two decimal places.
Output details
- Return columns in this order:
display_name, engagement_date, daily_engagement_minutes, running_total, moving_avg_3_days.
- Order rows by
display_name ascending, then engagement_date ascending.