Query editor
-- Supply Module-- Query 07-- Goal:-- - Build a daily Ethereum stablecoin supply panel-- - Use the latest available day to define the top 20 stablecoins by supply-- - Keep top 20 as separate series-- - Collapse all others into a single "Other" bucket---- Note:-- - This query is intentionally full-history-- - Top 20 is defined from the latest available day inside a recent 2-day window-- - This avoids scanning the full table just to locate max(day)WITH latest_day AS (SELECT max(day) AS dayFROM stablecoins_ethereum.balancesWHERE day >= current_date - INTERVAL '2' DAY),top20_latest AS (SELECTb.token_symbol,b.token_addressFROM stablecoins_ethereum.balances bINNER JOIN latest_day lON b.day = l.dayWHERE b.token_symbol IS NOT NULLAND b.token_symbol <> ''AND b.balance IS NOT NULLAND b.balance_usd IS NOT NULLGROUP BY 1, 2ORDER BY sum(b.balance_usd) DESCLIMIT 20),daily_token_supply AS (SELECTb.day,b.token_symbol,b.token_address,sum(b.balance) AS token_supply,sum(b.balance_usd) AS token_supply_usdFROM stablecoins_ethereum.balances bWHERE b.token_symbol IS NOT NULLAND b.token_symbol <> ''AND b.balance IS NOT NULLAND b.balance_usd IS NOT NULLGROUP BY 1, 2, 3),labeled_supply AS (SELECTd.day,CASEWHEN t.token_address IS NOT NULL THEN d.token_symbolELSE 'Other'END AS bucket_symbol,CASEWHEN t.token_address IS NOT NULL THEN CAST(d.token_address AS varchar)ELSE 'Other'END AS bucket_key,d.token_supply,d.token_supply_usd