Query editor
WITH trader_activity AS (-- Get unique trader-week combinations from 2025 onwardsSELECT DISTINCTevt_tx_from AS trader,DATE_TRUNC('week', evt_block_date) AS activity_weekFROM aerodrome_base.pool_evt_swapWHERE evt_block_date >= DATE '2025-01-01'AND evt_block_date <= CURRENT_DATE),trader_cohorts AS (-- Establish the first week a trader swappedSELECTtrader,MIN(activity_week) AS cohort_weekFROM trader_activityGROUP BY 1),cohort_sizes AS (-- Count baseline size of each weekly cohortSELECTcohort_week,COUNT(DISTINCT trader) AS cohort_sizeFROM trader_cohortsGROUP BY 1),retention_matrix AS (-- Calculate how many weeks later they swapSELECTc.cohort_week,a.activity_week,DATE_DIFF('week', c.cohort_week, a.activity_week) AS weeks_elapsed,COUNT(DISTINCT a.trader) AS retained_tradersFROM trader_cohorts cJOIN trader_activity a ON c.trader = a.traderGROUP BY 1, 2, 3)SELECTr.cohort_week AS cohort_start_date,'7-Day' AS cohort_type,s.cohort_size,-- LPAD dynamically turns 1 into '01', 2 into '02', but keeps 10 as '10'LPAD(CAST(r.weeks_elapsed AS VARCHAR), 2, '0') AS periods_elapsed,r.retained_traders,ROUND((CAST(r.retained_traders AS DOUBLE) / cohort_size) * 100, 2) AS retention_percentageFROM retention_matrix rJOIN cohort_sizes s ON r.cohort_week = s.cohort_weekWHERE r.weeks_elapsed >= 0ORDER BY r.cohort_week DESC, r.weeks_elapsed ASC;