Query editor
WITH gate AS (SELECT frozen_scale,hist_days,hist_days_ge_500,hist_worst_day_usd,hist_scaled_strict_zero_mark_usd,hist_scaled_capital_plus_gas_usd,oos_scaled_strict_net_usd,oos_scaled_peak_plus_gas_usdFROM dune.tusa3112.result_rh_top100_audited_prefilterWHERE LOWER(wallet)='0xcb4d28c2106fe9f781615005ff87056d114135c9'),e AS (SELECT * FROM dune.tusa3112.result_rh_top100_provenance_eventsWHERE wallet=0xcb4d28c2106fe9f781615005ff87056d114135c9),base AS (SELECT CAST(day AS VARCHAR) period,token,SUM(proceeds_usd-buy_cost_usd) gross_cash_usd,SUM(assigned_gas_usd) token_gas_usd,MIN(IF(event_class='PAID_BUY',block_time,NULL)) first_buy,MIN(IF(event_class='CASH_SELL',block_time,NULL)) first_sell,MAX(IF(event_class='CASH_SELL',block_time,NULL)) last_sell,SUM(IF(event_class IN ('PAID_BUY','UNPAID_IN'),token_in,0e0)-IF(event_class IN ('CASH_SELL','UNPAID_OUT'),token_out,0e0)) closing_qty,SUM(IF(event_class='UNPAID_IN',1,0)) unpaid_ins,SUM(IF(event_class='UNPAID_OUT',1,0)) unpaid_outsFROM e WHERE token IS NOT NULL GROUP BY 1,2UNION ALLSELECT 'ALL',token,SUM(proceeds_usd-buy_cost_usd),SUM(assigned_gas_usd),MIN(IF(event_class='PAID_BUY',block_time,NULL)),MIN(IF(event_class='CASH_SELL',block_time,NULL)),MAX(IF(event_class='CASH_SELL',block_time,NULL)),SUM(IF(event_class IN ('PAID_BUY','UNPAID_IN'),token_in,0e0)-IF(event_class IN ('CASH_SELL','UNPAID_OUT'),token_out,0e0)),SUM(IF(event_class='UNPAID_IN',1,0)),SUM(IF(event_class='UNPAID_OUT',1,0))FROM e WHERE token IS NOT NULL GROUP BY 2),periods AS (SELECT b.*,gross_cash_usd-token_gas_usd observed_contribution_usd,g.frozen_scale*gross_cash_usd-token_gas_usd frozen_contribution_usdFROM base b CROSS JOIN gate g),overhead AS (SELECT CAST(day AS VARCHAR) period,SUM(assigned_gas_usd) non_token_gasFROM e WHERE token IS NULL GROUP BY 1UNION ALL SELECT 'ALL',SUM(assigned_gas_usd) FROM e WHERE token IS NULL),s AS (SELECT p.period,COUNT(*) tokens,COUNT_IF(observed_contribution_usd>0e0) winning_tokens,SUM(observed_contribution_usd)-COALESCE(MAX(o.non_token_gas),0e0) observed_strict_zero_mark_usd,SUM(frozen_contribution_usd)-COALESCE(MAX(o.non_token_gas),0e0) frozen_strict_zero_mark_usd,MAX(frozen_contribution_usd) frozen_top1_token_contribution_usd,SUM(GREATEST(frozen_contribution_usd,0e0)) frozen_positive_contribution_usd,MAX(frozen_contribution_usd)/NULLIF(SUM(GREATEST(frozen_contribution_usd,0e0)),0e0) frozen_top1_positive_share,SUM(frozen_contribution_usd)-MAX(frozen_contribution_usd)-COALESCE(MAX(o.non_token_gas),0e0) frozen_ex_top1_strict_usd,SUM(unpaid_ins) unpaid_in_rows,SUM(unpaid_outs) unpaid_out_rows,MAX(ABS(closing_qty)) max_abs_closing_qty,APPROX_PERCENTILE(DATE_DIFF('second',first_buy,first_sell),0.5) median_first_exit_seconds,APPROX_PERCENTILE(DATE_DIFF('second',first_buy,last_sell),0.5) median_full_exit_seconds,APPROX_PERCENTILE(DATE_DIFF('second',first_buy,first_sell),0.1) p10_first_exit_seconds,APPROX_PERCENTILE(DATE_DIFF('second',first_buy,first_sell),0.9) p90_first_exit_secondsFROM periods p LEFT JOIN overhead o ON o.period=p.period GROUP BY 1)SELECT s.*,g.frozen_scale,IF(s.period='ALL',g.hist_days,NULL) hist_days,IF(s.period='ALL',g.hist_days_ge_500,NULL) hist_days_ge_500,IF(s.period='ALL',g.hist_worst_day_usd,NULL) hist_worst_day_usd,IF(s.period='ALL',g.hist_scaled_strict_zero_mark_usd,NULL) hist_gate_strict_usd,IF(s.period='ALL',g.hist_scaled_capital_plus_gas_usd,NULL) hist_frozen_peak_capital_usd,