Query editor
-- Monthly Summary: Month-over-month metrics-- Returns: Monthly aggregates for historical comparisonWITH pool_fees AS (SELECTpool,block_date,lp_fee,protocol_fee,ROW_NUMBER() OVER (PARTITION BY pool, block_date ORDER BY last_updated DESC) AS rnFROM ton.dex_poolsWHERE project = 'tonco'),monthly_trades AS (SELECTDATE_TRUNC('month', t.block_date) AS month_start,SUM(t.volume_usd) AS monthly_volume,SUM(t.volume_ton) AS monthly_volume_ton,COUNT(DISTINCT t.tx_hash) AS monthly_trades,COUNT(DISTINCT t.trader_address) AS monthly_traders,COUNT(DISTINCT t.pool_address) AS active_pools,AVG(t.volume_usd) AS avg_trade_size,approx_percentile(t.volume_usd, 0.5) AS median_trade_size,MAX(t.volume_usd) AS max_trade_size,SUM(t.volume_usd * COALESCE(pf.lp_fee + pf.protocol_fee, 0)) AS monthly_feesFROM ton.dex_trades tLEFT JOIN pool_fees pfON pf.pool = t.pool_addressAND pf.block_date = t.block_dateAND pf.rn = 1WHERE t.project = 'tonco'GROUP BY DATE_TRUNC('month', t.block_date)),monthly_tvl AS (SELECTDATE_TRUNC('month', block_date) AS month_start,AVG(tvl_usd) AS avg_tvl,MAX(tvl_usd) AS max_tvl,MIN(tvl_usd) AS min_tvlFROM (SELECTblock_date,SUM(tvl_usd) AS tvl_usdFROM (SELECTblock_date,pool,tvl_usd,ROW_NUMBER() OVER (PARTITION BY block_date, pool ORDER BY last_updated DESC) AS rnFROM ton.dex_poolsWHERE project = 'tonco') dedupedWHERE rn = 1GROUP BY block_date) daily_tvlGROUP BY DATE_TRUNC('month', block_date))