Query editor
WITH wallet_ltv AS (SELECTtrader_id AS wallet,MIN(block_time) AS first_trade_date, -- when wallet started tradingMAX(block_time) AS last_trade_date,COUNT(*) AS total_trades,SUM(amount_usd) AS total_volume_usd, -- gross volume generatedSUM(fee_usd) AS total_fees_paid_usd, -- revenue to protocolsCOUNT(DISTINCT DATE_TRUNC('day', block_time)) AS active_daysFROM dex_solana.tradesWHERE block_time >= NOW() - INTERVAL '90' dayAND amount_usd IS NOT NULLGROUP BY trader_idHAVING COUNT(*) >= 5 -- filter low-activity wallets)SELECTCOUNT(DISTINCT wallet) AS total_wallets_analyzed,ROUND(AVG(total_volume_usd), 0) AS avg_ltv_volume_usd, -- Average Lifetime Volume per WalletROUND(AVG(total_fees_paid_usd), 4) AS avg_ltv_fees_usd, -- Average Fees Paid (Protocol Revenue)ROUND(approx_percentile(total_volume_usd, 0.5), 0) AS median_ltv_volume_usd,ROUND(approx_percentile(total_volume_usd, 0.9), 0) AS p90_ltv_volume_usd, -- Top 10% walletsROUND(AVG(active_days), 1) AS avg_active_days,ROUND(SUM(total_volume_usd) / NULLIF(COUNT(DISTINCT wallet), 0), 0) AS overall_avg_ltvFROM wallet_ltv