Query editor
-- =====================================================================================-- STAGE 5 — cash-out payoff (+ de-batched treasury attribution)-- =====================================================================================WITH d AS (SELECT * FROM query_7998122), wd AS (SELECT * FROM query_8002550),ent AS (SELECT blockchain,address,array_join(array_agg(DISTINCT name),', ') entity_names FROM labels.addresses GROUP BY 1,2),cex AS (SELECT blockchain,address,array_join(array_agg(DISTINCT name),', ') cex_name FROM labels.addresses WHERE label_type='cex' GROUP BY 1,2),ens AS (SELECT address, name AS ens_name FROM ens.reverse_latest)SELECT fe.entity_names AS treasury, d.sender,d.recipient, re.ens_name AS recipient_ens,w.withdraw_to, cx.cex_name AS withdrew_to_cex, we.ens_name AS withdraw_to_ens,COUNT(*) AS num_withdrawals, SUM(w.amount)/1e18 AS total_withdrawn,MIN(w.evt_block_time) AS first_wd, MAX(w.evt_block_time) AS last_wdFROM dJOIN wd w ON w.stream_id = d.stream_id AND w.contract_address = d.contract_addressLEFT JOIN ens re ON re.address = d.recipientLEFT JOIN ens we ON we.address = w.withdraw_toLEFT JOIN cex cx ON cx.address = w.withdraw_to AND cx.blockchain='ethereum'LEFT JOIN ent fe ON fe.address = d.effective_funder AND fe.blockchain='ethereum'-- WHERE d.token = 0xc770eefad204b5180df6a14ee197d99d808ee52d -- Set the token address to refine the resultGROUP BY 1,2,3,4,5,6,7ORDER BY num_withdrawals DESC;