Query editor
with legs as (selectf.*,abs(casewhen subbet_result = 'cancelled' then 1elsecasewhen cond_status = 'active' then nullwhen cond_status = 'resolved' thencasewhen subbet_result = 'won' then condition_oddswhen subbet_result = 'lost' then 0endendend) as odds_termfrom query_5072320 f),bets_prep as (selectorder_id,bet_id,max(chain) as chain,max(bettor) as bettor,max(bet_time) as bet_time,max(affiliate) as affiliate,max(coalesce(frontend, 'unknown')) as frontend,max(amount) as amount,max(case when bet_type = 1 then 'combo' when bet_type = 0 then 'ordinar' end) as bet_type,exp(sum(ln(abs(condition_odds)))) as initial_odds,case when bool_or(odds_term is null) then nullelse exp(sum(ln(odds_term)))end as true_odds,case when bool_or(odds_term is null) then nullelse 1 - exp(sum(ln(abs(nullif(1 - true_subbet_margin, 0)))))end as true_margin,1 - exp(sum(ln(abs(nullif(1 - cond_margin, 0))))) as initial_margin,case when bool_or(odds_term is null) then 'accepted' else 'resolved' end as bet_status,case when max(coalesce(subbet_resolve_time, cast('2100-01-01' as date))) = cast('2100-01-01' as date) then nullelse max(subbet_resolve_time)end as resolve_time,bool_or(is_freebet) as is_freebet,bool_or(is_sponsored_bet_returnable) as is_sponsored_bet_returnablefrom legsleft join dune.azuro.dataset_frontends using (affiliate)group by 1, 2),prices as (selectd.day,last_value(p.eth_price) ignore nullsover (order by d.day rows between unbounded preceding and current row) as eth_pricefrom unnest(sequence(date '2022-01-01', current_date, interval '1' day)) as d(day)left join query_3489370 p on cast(p.date as date) = d.day),prep as (selectb.*,