Query editor
-- =====================================================================-- defi_overview__kpis__ytd-- Aggregate KPIs with YTD and YoY changes.-- Stock metrics: latest value vs Jan 1 / 1y ago.-- Flow metrics: 30d avg vs first-30d avg / 1y-ago 30d avg.-- =====================================================================WITH base AS (SELECTdate, tvl, dex_volume, fees, stablecoin_mcap, open_interest, dex_volume / NULLIF(tvl, 0) AS velocity, CASEWHEN (tvl + COALESCE(open_interest, 0) * 0.20) > 1e6THEN fees / (tvl + COALESCE(open_interest, 0) * 0.20)ELSE NULLEND AS fee_efficiency, open_interest / NULLIF(tvl, 0) AS oi_to_tvlFROM dune.ghrjeondata.result_defi_overview_daily_metricsWHERE tvl IS NOT NULL), latest AS (SELECT * FROM base ORDER BY date DESC LIMIT 1), ytd_start AS (SELECT * FROM baseWHERE date >= CAST('2026-01-01' AS timestamp)ORDER BY date LIMIT 1), yoy_ref AS (SELECT * FROM baseWHERE date >= DATE_ADD('year', -1, (SELECT date FROM latest))ORDER BY date LIMIT 1)-- 30d averages for flow metrics, avg_recent AS (SELECTAVG(dex_volume) AS avg_volume, AVG(fees) AS avg_fees, AVG(velocity) AS avg_velocity, AVG(fee_efficiency) AS avg_fee_efficiencyFROM baseWHERE date > DATE_ADD('day', -30, (SELECT date FROM latest))), avg_ytd_start AS (SELECTAVG(dex_volume) AS avg_volume, AVG(fees) AS avg_fees, AVG(velocity) AS avg_velocity, AVG(fee_efficiency) AS avg_fee_efficiencyFROM baseWHERE date >= CAST('2026-01-01' AS timestamp)AND date < CAST('2026-01-31' AS timestamp)), avg_yoy AS (SELECTAVG(dex_volume) AS avg_volume