I stared at this for a solid minute before writing anything.
Start by clarifying the schema and business rules, then break the problem into logical CTEs: identity resolution, user eligibility, activity aggregation, and retention calculation. Use window functions to compute first activity, previous activity, and gap-based retention flags, ensuring the 60-day rule is applied cumulatively. Finally, pivot the results into a cohort retention matrix.
Pro tip: Explicitly state your assumptions about the merge logic (e.g., parent-child relationships, cycles) and the retention rule (e.g., gap measured from previous non-refund event, not previous activity). This shows you think about edge cases and data quality, which is crucial for a production-grade query.
Ask about table structures, merge semantics (e.g., parent-child direction, cycles), and definitions of eligible users, activity, and refund events. Confirm the retention rule details: gap threshold, reference event, and cumulative 60-day rule.
Use recursive CTEs to map all child identities to their ultimate parent, then join to user profiles to filter for US, non-employee, non-test users. Ensure events from merged identities are attributed to the parent.
For each user and month, determine if they had a qualifying event (app_open or purchase). Use window functions to find the first qualifying event per month and the previous non-refund event, then compute the gap in days.
Flag a user as retained in month k if the gap between their first qualifying event that month and their previous non-refund event is ≤35 days. Use a running window to track if a 60-day gap has occurred; if so, mark all subsequent months as not retained.
For cohorts Jan-Mar 2025, compute retention for k=0 to k=3 by joining the cohort definition to the retention flags. Use conditional aggregation to pivot months into columns.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.