This one took me a minute to even set up mentally.
First, define the cohort month for each user as the month of their signup date, then join user_activity to count distinct active users per cohort per month offset. Aggregate to get cohort size and retained users, then compute retention rate as retained users divided by cohort size, filtering for offsets 0 through 6.
Pro tip: Clarify the definition of 'active' (e.g., any activity vs. specific action) and handle edge cases like users with no activity in month 0, as retention is typically measured from the first activity month, not signup month.
Identify the cohort month (e.g., DATE_TRUNC('month', signup_date)) for each user and define what constitutes an active user in a given month (e.g., any record in user_activity).
For each user, determine the distinct months they were active by truncating activity dates to month.
For each user and each active month, compute the number of months since their cohort month (offset), ensuring offsets range from 0 to 6.
Group by cohort month and offset, count distinct users as retained users, and get cohort size as the number of users in that cohort (constant across offsets).
Calculate retention rate as retained_users / cohort_size, and select the required columns: cohort month, months since signup, cohort size, retained users, and retention rate.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.