Write a complex SQL query using window functions (e.g., DENSE_RANK, LAG, LEAD) to calculate 7-day rolling user engagement metrics across multiple product categories at Apple. Use the users, product_categories, and engagement_events tables. Include completed events from January 1 through January 10, 2025, treating a NULL engagement score as zero.
Output
- One row per user, category, and activity date.
- Return
user_id, category_name, activity_date, daily_engagement, rolling_7d_engagement, previous_rolling_7d, next_rolling_7d, and category_rank.
- Rank rows by rolling engagement within each activity date, with ties sharing a rank. Order by activity date, rank, user ID, and category name.