Query editor
with okx_ethereum_tx as (-- Added DISTINCT to ensure 1 hash = 1 rowselect distinct 'okx' as project, tx_hashfrom dune.okxweb3wallet.result_okx_eth_txwhere block_time >= TIMESTAMP '{{block_time_start}}'and block_time <= TIMESTAMP '{{block_time_end}}'), okx_bnb_tx as (select distinct 'okx' as project, tx_hashfrom dune.okxweb3wallet.result_dex_project_volume_bscwhere block_time >= TIMESTAMP '{{block_time_start}}'and block_time <= TIMESTAMP '{{block_time_end}}')-- , okx_arbitrum_tx as (-- select distinct 'okx' as project, tx_hash-- from query_4899526-- where block_time >= TIMESTAMP '{{block_time_start}}'-- and block_time <= TIMESTAMP '{{block_time_end}}'-- )-- , okx_base_tx as (-- select distinct 'okx' as project, tx_hash-- from query_4899451-- where block_time >= TIMESTAMP '{{block_time_start}}'-- and block_time <= TIMESTAMP '{{block_time_end}}'-- )-- CRITICAL NEW CTE: De-dupe aggregator trades, unique_aggregator as (selecttx_hash, arbitrary(project) as project -- Picks one project if duplicates existfrom dex_aggregator.tradeswhere block_time >= TIMESTAMP '{{block_time_start}}'and block_time <= TIMESTAMP '{{block_time_end}}'group by 1)selectIF(dat.project is null, IF(okx.project is null, 'unknown', okx.project), dat.project) as aggregator, dt.blockchain, date_trunc('{{granularity}}', dt.block_time) as block_time, SUM(dt.amount_usd) as amount_usdfrom dex.trades dt-- Join to the CLEANED aggregator CTE, not the raw tableleft join unique_aggregator dat on dt.tx_hash = dat.tx_hash-- Join to the CLEANED OKX CTE (using the variable logic)left join okx_{{blockchain}}_tx okx on dt.tx_hash = okx.tx_hashwhere dt.project = 'native'and dt.block_time >= TIMESTAMP '{{block_time_start}}'and dt.block_time <= TIMESTAMP '{{block_time_end}}'and dt.blockchain = '{{blockchain}}'group by 1, 2, 3order by block_time desc