Query editor
WITH dex_trades AS (SELECTblockchain,project,tx_from AS user,block_time,token_pair,amount_usdFROM dex.trades-- WHERE block_time >= DATE_TRUNC('day', NOW() - INTERVAL '1' YEAR)WHERE block_month >= DATE_TRUNC('day', NOW() - INTERVAL '1' YEAR)AND amount_usd > 0AND tx_from NOT IN (SELECT address FROM evms.creation_traces) --remove all bots/non eoas--AND blockchain = 'ethereum' --remove this filter to get data on many more chains. Requires paid plan to run the query.),user_metrics AS (SELECTuser,blockchain,project,MIN(block_time) AS first_tx_date,MAX(block_time) AS last_tx_date,COUNT(*) AS total_trades,SUM(amount_usd) AS total_volume_usd,SUM(CASE WHEN block_time >= DATE_TRUNC('day', NOW() - INTERVAL '3' MONTH) THEN amount_usd ELSE 0 END) AS volume_usd_last_3_months,SUM(CASE WHEN block_time >= DATE_TRUNC('day', NOW() - INTERVAL '6' MONTH) THEN amount_usd ELSE 0 END) AS volume_usd_last_6_monthsFROM dex_tradesGROUP BY 1, 2, 3HAVING SUM(amount_usd) > 1000),filtered_metrics AS (SELECTCAST(user AS VARCHAR) as address,'dex' as sector,ARRAY_AGG(DISTINCT blockchain ORDER BY blockchain) AS blockchain,ARRAY_AGG(DISTINCT project ORDER BY project) AS project,COUNT(DISTINCT project) AS unique_dex_count,MIN(first_tx_date) AS first_tx_date,MAX(last_tx_date) AS last_tx_date,SUM(total_trades) AS total_trades,ROUND(SUM(total_volume_usd), 2) AS total_volume_usd,DATE_DIFF('day', MIN(first_tx_date), MAX(last_tx_date)) AS days_active,ROUND(SUM(volume_usd_last_3_months), 2) AS volume_usd_last_3_months,ROUND(SUM(volume_usd_last_6_months), 2) AS volume_usd_last_6_monthsFROM user_metricsGROUP BY 1),results as (SELECT*,ROUND(CAST(total_trades AS DOUBLE) / NULLIF(days_active, 0), 4) AS avg_daily_trades,ROUND(total_volume_usd / NULLIF(total_trades, 0), 2) AS avg_trade_size_usd,-- ROUND(100 * CUME_DIST() OVER (ORDER BY total_volume_usd), 2) AS total_volume_percentile, --Used CUME_DIST() instead of PERCENT_RANK() for percentile calculations. This is functionally similar but can be more efficient in some cases.{{unnamed_parameter}}{{unnamed_parameter_2}}-- ROUND(100 * CUME_DIST() OVER (ORDER BY total_trades), 2) AS total_trades_percentile,-- ROUND(100 * CUME_DIST() OVER (ORDER BY days_active), 2) AS days_active_percentile,-- ROUND(100 * CUME_DIST() OVER (ORDER BY (CAST(total_trades AS DOUBLE) / NULLIF(days_active, 0))), 2) AS avg_daily_trades_percentile,-- ROUND(100 * CUME_DIST() OVER (ORDER BY volume_usd_last_3_months), 2) AS volume_last_3_months_percentile,-- ROUND(100 * CUME_DIST() OVER (ORDER BY volume_usd_last_6_months), 2) AS volume_last_6_months_percentile