Query editor
-- Query 4 (optimized): Net flow of DAI by entity bucket over the last 30 days-- Strategy: aggregate transfers per address first, then join a deduped labels table.-- Units: DAIWITH-- 1) aggregate total inflows per receiving address (to) over the windowagg_inflows AS (SELECT"to" AS addr, -- varbinarySUM(value) / 1e18 AS total_inflow_daiFROM erc20_ethereum.evt_TransferWHERE contract_address = FROM_HEX('6B175474E89094C44DA98B954EEDEAC495271D0F')AND evt_block_time >= CURRENT_TIMESTAMP - INTERVAL '30' dayGROUP BY 1),-- 2) aggregate total outflows per sending address (from) over the windowagg_outflows AS (SELECT"from" AS addr, -- varbinarySUM(value) / 1e18 AS total_outflow_daiFROM erc20_ethereum.evt_TransferWHERE contract_address = FROM_HEX('6B175474E89094C44DA98B954EEDEAC495271D0F')AND evt_block_time >= CURRENT_TIMESTAMP - INTERVAL '30' dayGROUP BY 1),-- 3) create a deduplicated labels table (one row per address)labels_unique AS (SELECTaddress, -- varbinaryMAX(name) AS name,MAX(category) AS categoryFROM labels.addressesGROUP BY address),-- 4) join aggregated flows to labels (left join so unlabeled are 'other')inflows_labeled AS (SELECTCOALESCE(lu.category, 'unlabeled') AS category,ai.total_inflow_daiFROM agg_inflows aiLEFT JOIN labels_unique lu ON ai.addr = lu.address),outflows_labeled AS (SELECTCOALESCE(lu.category, 'unlabeled') AS category,ao.total_outflow_daiFROM agg_outflows aoLEFT JOIN labels_unique lu ON ao.addr = lu.address),-- 5) map raw label categories into our broad buckets (cex / defi / bridge / other)map_inflows AS (SELECTCASEWHEN LOWER(COALESCE(category, '')) LIKE '%cex%' THEN 'cex'WHEN LOWER(COALESCE(category, '')) LIKE '%bridge%' THEN 'bridge'