Query editor
WITH calculated_fees AS (SELECTpair_name,group_name,COALESCE(position_size_dai, 0) * COALESCE(collateralpriceusd, 0) AS collateral_usd,COALESCE(position_size_dai, 0)* ABS(COALESCE(leverage, 0))* COALESCE(collateralpriceusd, 0) AS volume_usd,COALESCE(pnl, 0) * COALESCE(collateralpriceusd, 0) AS pnl_usd,COALESCE(pnl_raw, 0) * COALESCE(collateralpriceusd, 0) AS pnl_post_price_impact_usd,(COALESCE(project_fund, 0)+ COALESCE(dev_fund, 0)+ COALESCE(referral, 0)+ COALESCE(nft_bots, 0)+ COALESCE(dai_stakers, 0)+ COALESCE(lp_stakers, 0)+ COALESCE(gns_stakers, 0)+ COALESCE(oc_fees, 0)) * COALESCE(collateralpriceusd, 0) AS trading_fees_usd,(COALESCE(borrowing_fee, 0)+ COALESCE(funding_fees, 0)) * COALESCE(collateralpriceusd, 0) AS holding_fees_usd,COALESCE(gns_stakers, 0) * COALESCE(collateralpriceusd, 0) AS burn_fee_usd,COALESCE(oc_fees, 0) * COALESCE(collateralpriceusd, 0) AS oc_fees_usd,(priceimpactp / 1e12)* COALESCE(position_size_dai, 0)* ABS(COALESCE(leverage, 0))* COALESCE(collateralpriceusd, 0) AS signed_price_impact_usd,CASEWHEN version NOT LIKE '10%' THEN 1WHEN (type IN ('OPEN','LIMIT')AND ((long = true AND priceimpactp > 0) OR(long = false AND priceimpactp < 0))) OR (type IN ('CLOSE','TP','SL','LIQ')AND ((long = true AND priceimpactp < 0) OR(long = false AND priceimpactp > 0)))THEN 1 ELSE 0END AS is_price_impact_costFROM dune.gains.result_gtrade_all_orders_daily_viewWHERE block_time >= TRY_CAST('2026-07-01' AS TIMESTAMP)),final AS (SELECT*,CASEWHEN is_price_impact_cost = 1THEN LEAST(ABS(signed_price_impact_usd), collateral_usd)ELSE 0END AS price_impact_cost_usd,CASEWHEN is_price_impact_cost = 0