Query editor
-- =====================================================-- DASHBOARD KPI AGGREGATES-- Reads from the Private Activity MV-- =====================================================WITH base_data AS (-- 1. Pull the filtered data based on user dashboard parametersSELECTday,blockchain,asset_type,operation,total_private_transactions,unique_users,gross_deposit_usd,gross_withdrawal_usd,net_tvl_usdFROM dune.igboweefe.result_private_activity_mvWHERE day >= COALESCE(TRY_CAST('{{Start Date}}' AS TIMESTAMP), TIMESTAMP '2024-01-01 00:00:00')AND day < COALESCE(TRY_CAST('{{End Date}}' AS TIMESTAMP), CURRENT_TIMESTAMP)-- Applies the Blockchain filter, defaulting to 'All'AND ('{{Blockchain}}' = 'All' OR blockchain = '{{Blockchain}}')),deduped_volume AS (-- 2. DEDUPLICATION FIX: Because the MV splits rows by "operation", proxy volumes are duplicated per day.-- We group by day, chain, and asset to take the MAX value of each metric before summing.SELECTday,blockchain,asset_type,MAX(gross_deposit_usd) AS daily_deposit,MAX(gross_withdrawal_usd) AS daily_withdrawal,MAX(net_tvl_usd) AS daily_tvlFROM base_dataGROUP BY 1, 2, 3)-- 3. Calculate all the Top-Level KPIs in a single output rowSELECTCOALESCE((SELECT SUM(daily_deposit) FROM deduped_volume), 0) AS total_deposits_usd,COALESCE((SELECT SUM(daily_withdrawal) FROM deduped_volume), 0) AS total_withdrawals_usd,COALESCE((SELECT SUM(daily_tvl) FROM deduped_volume), 0) AS current_net_tvl_usd,COALESCE((SELECT SUM(total_private_transactions) FROM base_data), 0) AS total_transaction_count,COALESCE((SELECT SUM(unique_users) FROM base_data), 0) AS total_unique_users,(SELECT COUNT(DISTINCT blockchain) FROM base_data) AS total_chains,(SELECT COUNT(DISTINCT asset_type) FROM base_data) AS total_assets;