Query editor
with base as (select 'USD1' as episode, blockchain, block_time, token_bought_address, token_sold_address, token_bought_amount, token_sold_amount,0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d as tknfrom dex.tradeswhere blockchain in ('ethereum','bnb') and block_date between date '2026-11-21' and date '2026-11-30'and (token_bought_address = 0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d or token_sold_address = 0x8d0d000ee44948fc98c9b98a4fa4921476f08b0d)and (token_bought_address in (0x55d398326f99059ff775485246999027b3197955, 0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d, 0xdac17f958d2ee523a2206206994597c13d831ec7, 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48)or token_sold_address in (0x55d398326f99059ff775485246999027b3197955, 0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d, 0xdac17f958d2ee523a2206206994597c13d831ec7, 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48))union allselect 'USDC', blockchain, block_time, token_bought_address, token_sold_address, token_bought_amount, token_sold_amount,0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48from dex.tradeswhere blockchain = 'ethereum' and block_date between date '2023-03-08' and date '2023-03-17'and (token_bought_address = 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48 or token_sold_address = 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48)and (token_bought_address = 0xdac17f958d2ee523a2206206994597c13d831ec7 or token_sold_address = 0xdac17f958d2ee523a2206206994597c13d831ec7)union allselect 'UST', blockchain, block_time, token_bought_address, token_sold_address, token_bought_amount, token_sold_amount,case when token_bought_address in (0xa693b19d2931d498c5b318df961919bb4aee87a5, 0xa47c8bf37f92abed4a126bda807a7b7498661acd) then token_bought_address else token_sold_address endfrom dex.tradeswhere blockchain = 'ethereum' and block_date between date '2022-05-05' and date '2022-05-31'and (token_bought_address in (0xa693b19d2931d498c5b318df961919bb4aee87a5, 0xa47c8bf37f92abed4a126bda807a7b7498661acd)or token_sold_address in (0xa693b19d2931d498c5b318df961919bb4aee87a5, 0xa47c8bf37f92abed4a126bda807a7b7498661acd))and (token_bought_address in (0xdac17f958d2ee523a2206206994597c13d831ec7, 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48, 0x6b175474e89094c44da98b954eedeac495271d0f)or token_sold_address in (0xdac17f958d2ee523a2206206994597c13d831ec7, 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48, 0x6b175474e89094c44da98b954eedeac495271d0f))),t as (select episode, block_time,case when token_bought_address = tkn then token_bought_amount else token_sold_amount end as amt,case when token_bought_address = tkn then token_sold_amount else token_bought_amount end as quote_amtfrom base),h as (select episode, date_trunc('hour', block_time) as hour, sum(quote_amt)/sum(amt) as vwap, sum(amt) as volfrom t where amt > 0 and quote_amt > 0 group by 1,2),f as (select episode, min(case when vwap < 0.99 then hour end) as first_below from h group by 1)select h.episode, f.first_below,min(h.vwap) as low_vwap, min_by(h.hour, h.vwap) as low_hour,date_diff('hour', f.first_below, min_by(h.hour, h.vwap)) as hours_to_low,max(case when h.vwap < 0.99 then h.hour end) as last_below_in_window,count_if(h.vwap < 0.99) as hours_below_99c,count_if(h.vwap < 0.95) as hours_below_95c,min(case when h.hour >= f.first_below and h.hour < f.first_below + interval '7' day then h.vwap end) as low_first_7d,count_if(h.vwap < 0.99 and h.hour < f.first_below + interval '7' day) as hours_below_99c_first_7d,max_by(h.vwap, h.hour) as last_vwap_in_window, max(h.hour) as window_end,sum(case when h.hour >= f.first_below and h.hour < f.first_below + interval '24' hour then h.vol end) as volume_first_24hfrom h join f on f.episode = h.episodegroup by 1,2 order by 1