Query editor
-- Find the first transaction week for each unique addressWITH address_first_tx AS (SELECT"from" AS address,MIN(date_trunc('week', block_time)) AS first_tx_weekFROM base.transactionsGROUP BY 1),-- Generate a list of all unique weeks in the datasetall_weeks AS (SELECT DISTINCT date_trunc('week', block_time) AS weekFROM base.transactions),-- Create a cross join between addresses and weeks,-- ensuring we have a row for each address for every week since their first transactionaddress_week_cross AS (SELECTa.address,w.week,a.first_tx_weekFROM address_first_tx aCROSS JOIN all_weeks wWHERE w.week >= a.first_tx_week),-- Calculate the number of transactions for each address in each weekweekly_activity AS (SELECTdate_trunc('week', block_time) AS week,"from" AS address,COUNT(*) AS tx_countFROM base.transactionsGROUP BY 1, 2),-- Combine the cross join with weekly activity data and calculate key metricsaddress_history AS (SELECTc.address,c.week,c.first_tx_week,COALESCE(w.tx_count, 0) AS tx_count,-- Track the last week of activity for each addressMAX(CASE WHEN COALESCE(w.tx_count, 0) > 0 THEN c.week END) OVER (PARTITION BY c.address ORDER BY c.week ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS last_active_week,-- Identify weeks where an address returned after 4 weeks of inactivityCASE WHENCOALESCE(w.tx_count, 0) > 0 ANDCOALESCE(LAG(w.tx_count, 1) OVER (PARTITION BY c.address ORDER BY c.week), 0) = 0 ANDCOALESCE(LAG(w.tx_count, 2) OVER (PARTITION BY c.address ORDER BY c.week), 0) = 0 ANDCOALESCE(LAG(w.tx_count, 3) OVER (PARTITION BY c.address ORDER BY c.week), 0) = 0 ANDCOALESCE(LAG(w.tx_count, 4) OVER (PARTITION BY c.address ORDER BY c.week), 0) = 0THEN c.week ELSE NULL END AS return_weekFROM address_week_cross cLEFT JOIN weekly_activity w ON c.address = w.address AND c.week = w.week),-- Keep track of the most recent return week for each address at any given point in timeaddress_return_weeks AS (