Query editor
WITH deduped_flows AS (SELECT *,ROW_NUMBER() OVER (PARTITION BY deposit_tx_hash, withdrawal_tx_hashORDER BY amount_usd DESC) AS rnFROM bridges_crosschain.flowsWHERE ((withdrawal_chain = 'base' AND withdrawal_block_time >= NOW() - INTERVAL '{{days}}' DAY)OR(deposit_chain = 'base' AND deposit_block_time >= NOW() - INTERVAL '{{days}}' DAY))),base_flows AS (SELECTDATE_TRUNC('{{granularity}}', withdrawal_block_time) AS "date",COALESCE(deposit_chain, 'unknown') AS chain,bridge_name,'inflow' AS flow_direction,amount_usdFROM deduped_flowsWHERE rn = 1AND withdrawal_chain = 'base'AND amount_usd > 0UNION ALLSELECTDATE_TRUNC('{{granularity}}', deposit_block_time) AS "date",COALESCE(withdrawal_chain, 'unknown') AS chain,bridge_name,'outflow' AS flow_direction,amount_usdFROM deduped_flowsWHERE rn = 1AND deposit_chain = 'base'AND amount_usd > 0),aggregated_flows AS (SELECT"date",chain,SUM(CASE WHEN flow_direction = 'inflow' THEN amount_usd ELSE 0 END) AS total_inflow_usd,COUNT(CASE WHEN flow_direction = 'inflow' THEN 1 END) AS total_inflow_count,SUM(CASE WHEN flow_direction = 'outflow' THEN amount_usd ELSE 0 END) AS total_outflow_usd,COUNT(CASE WHEN flow_direction = 'outflow' THEN 1 END) AS total_outflow_countFROM base_flowsGROUP BY 1, 2)SELECT"date",chain,total_inflow_usd,total_inflow_count,total_outflow_usd,total_outflow_count,(total_inflow_usd - total_outflow_usd) AS net_flow_usd,(total_inflow_count - total_outflow_count) AS net_flow_countFROM aggregated_flowsORDER BY "date" DESC, net_flow_usd DESC;