Query editor
WITH withdrawals AS (SELECT w.block_time, CASE WHEN amount/1e9 BETWEEN 20 AND 32 THEN amount/1e9 WHEN amount/1e9 > 32 THEN 32 ELSE 0 END AS withdrawn_principal, CASE WHEN amount/1e9 < 20 THEN amount/1e9 WHEN amount/1e9 > 32 THEN (amount/1e9)-32 ELSE 0 END AS withdrawn_rewards, w.amount/1e9 AS total_withdrawn, w.address, w.validator_index, CASE WHEN idx.entity = 'Abyss Finance' THEN NULL ELSE idx.entity END AS withdrawing_entity, idx.entity_category AS withdrawing_entity_categoryFROM ethereum.withdrawals wINNER JOIN query_2364548 idx ON w.validator_index=idx.validator_indexWHERE w.block_time > NOW() - interval '3' month), inflow AS (SELECT CASE WHEN entity IS NOT NULL AND entity != 'Abyss Finance' THEN ROW_NUMBER() OVER (ORDER BY SUM(amount_staked) DESC) END AS ranking, entity, SUM(amount_staked) AS eth_inflowFROM staking_ethereum.depositsWHERE block_time > NOW() - interval '3' monthGROUP BY 2), outflow AS (SELECT CASE WHEN withdrawing_entity IS NOT NULL THEN ROW_NUMBER() OVER(ORDER BY SUM(total_withdrawn) DESC) END AS ranking, withdrawing_entity AS entity, SUM(total_withdrawn) AS eth_outflow, SUM(withdrawn_principal) AS eth_principal_outflow, SUM(withdrawn_rewards) AS eth_rewards_outflowFROM withdrawalsWHERE total_withdrawn > 0GROUP BY 2)SELECT CASE WHEN d.entity = 'Kiln' THEN get_href('https://x.com/Kiln_finance', d.entity)WHEN d.entity = 'Diva (Pre-launch)' THEN get_href('https://x.com/divastaking', d.entity)WHEN info.x_username IS NOT NULL THEN get_href('https://x.com/' || info.x_username, d.entity)WHEN d.entity IS NULL THEN 'Unidentified'ELSE d.entity END AS entity, d.eth_inflow--, eth_outflow, -d.eth_principal_outflow AS eth_principal_outflow, -d.eth_rewards_outflow AS eth_rewards_outflow, d.net_inflow, d.net_inflow_excluding_rewardsFROM (SELECT CASE WHEN i.entity IS NOT NULL THEN ROW_NUMBER() OVER(ORDER BY COALESCE(i.eth_inflow, 0)-COALESCE(o.eth_outflow, 0) DESC) END AS ranking, CASE WHEN i.entity IS NOT NULL THEN ROW_NUMBER() OVER(ORDER BY COALESCE(i.eth_inflow, 0)-COALESCE(o.eth_principal_outflow, 0) DESC) END AS ranking_excl_rewards, CASE WHEN i.entity = 'Abyss Finance' THEN NULL ELSE i.entity END AS entity, COALESCE(i.eth_inflow, 0) AS eth_inflow, COALESCE(o.eth_outflow, 0) AS eth_outflow, COALESCE(o.eth_principal_outflow, 0) AS eth_principal_outflow, COALESCE(o.eth_rewards_outflow, 0) AS eth_rewards_outflow, COALESCE(i.eth_inflow, 0)-COALESCE(o.eth_outflow, 0) AS net_inflow, COALESCE(i.eth_inflow, 0)-COALESCE(o.eth_principal_outflow, 0) AS net_inflow_excluding_rewardsFROM inflow iLEFT OUTER JOIN outflow o ON i.entity=o.entityWHERE i.entity IS NOT NULLAND o.entity IS NOT NULL) d