How would you use SQL window functions to calculate rolling 7-day engagement and rank clients within each segment?
Use the provided client, segment, and engagement event data. Return results for clients assigned to a valid segment and dates on which they have engagement events.
Output
- One row per client, segment, and activity date.
- Columns:
activity_date, segment_name, client_id, client_name, rolling_7_day_engagement, and segment_rank.
- Rank clients within the same segment and activity date by rolling engagement descending, with ties sharing a rank and
client_id as the final ordering key.
Output details
- Sum all engagement events for each client on each activity date before calculating rolling engagement.
- Calculate rolling engagement over the inclusive calendar interval from
activity_date minus 6 days through activity_date.
- Order results by
activity_date ascending, segment_name ascending, segment_rank ascending, then client_id ascending.