Query editor
WITH totals AS (SELECT CASE WHEN SUM(unpriced_events)=0 AND SUM(duplicate_events)=0 THEN SUM(volume_usd) END AS total,SUM(binance_volume_usd) AS binance, SUM(u_pair_volume_usd) AS u_pair,SUM(binance_u_pair_volume_usd) AS binance_u, SUM(official_pool_volume_usd) AS official,SUM(estimated_volume_usd) AS estimated, MAX(through_date_utc) AS through_date_utc,SUM(unpriced_events) AS unpriced_events, SUM(duplicate_events) AS duplicate_eventsFROM dune.paimonfinance.result_ppoly_market_daily), scopes AS (SELECT totals.*,s.*, CASE sort_order WHEN 1 THEN total WHEN 2 THEN u_pairWHEN 3 THEN binance WHEN 4 THEN binance_u ELSE official END AS cumulative_volume_usdFROM totals CROSS JOIN (VALUES(1,'All tracked AMM pools','Total'),(2,'U-pPOLY pairs','Subset of total'),(3,'Binance router / all pairs','Subset of total'),(4,'Binance router / U-pPOLY','Binance router + U-pPOLY'),(5,'Official Infinity CL pool','Subset of U-pPOLY pairs')) AS s(sort_order,scope,relation))SELECT scope, relation, cumulative_volume_usd, cumulative_volume_usd/NULLIF(total,0) AS share_of_total,total AS total_cumulative_volume_usd, binance AS binance_cumulative_volume_usd,binance/NULLIF(total,0) AS binance_share, 100.0*binance/NULLIF(total,0) AS binance_share_pct, estimated/NULLIF(total,0) AS estimated_share,through_date_utc,unpriced_events,duplicate_eventsFROM scopes ORDER BY sort_order