Query editor
WITH time_series AS (SELECT DISTINCTDATE_TRUNC('month', time) AS monthFROM (SELECT timeFROM UNNEST(SEQUENCE(CAST('2024-06-01' AS TIMESTAMP),CAST(DATE_TRUNC('month', NOW()) AS TIMESTAMP),INTERVAL '1' MONTH)) AS s(time)WHERE time >= DATE_TRUNC('month', NOW() - INTERVAL '12' MONTH))),data AS (SELECT DISTINCTDATE_TRUNC('month', block_time) AS month,userFROM fantasy.eventsCROSS JOIN UNNEST(ARRAY[user_address, traded_with]) AS t(user)WHERE block_time >= DATE_TRUNC('month', NOW() - INTERVAL '12' MONTH)),user_cohort AS (-- Assign each user to the first month they were active (cohort_month)SELECTuser,MIN(month) AS cohort_monthFROM dataGROUP BY 1),monthly_active_users AS (-- Track users' activity in different monthsSELECTmonth AS active_month,userFROM dataGROUP BY 1, 2),retention AS (-- Count how many users from each cohort are active in future monthsSELECTu.cohort_month,m.active_month,COUNT(DISTINCT m.user) AS retained_usersFROM user_cohort uJOIN monthly_active_users mON u.user = m.userAND m.active_month >= u.cohort_month -- Only include future monthsGROUP BY 1, 2),cohort_size AS (-- Get total number of users in each cohortSELECTcohort_month,COUNT(DISTINCT user) AS cohort_sizeFROM user_cohort