Query editor
witha_before as(select lower(token_bought_mint_address) as token_bought_mint_address,date_trunc('hour',cast(block_time as timestamp)) as day from--dune.kelper.dataset_loss_sim-- query_4680492-- dune.yieldfarmers.result_q_3_opp_feed_simulation_supply--where (max_price_2hrs - token_buy_price)/max_price_2hrs <= 80-- query_4526561 main--dune.yieldfarmers.result_q_3_v_2dune.yieldfarmers.result_q_3_v_2_cum),price_4h_before AS (SELECTph.contract_address,DATE_TRUNC('hour', ph.hour) AS hour,MAX(ph.price) AS price_4h_beforeFROM dex_solana.price_hour phWHERE ph.hour >= (SELECT MIN(day) FROM a_before) - INTERVAL '4' hourAND ph.hour <= (SELECT MAX(day) FROM a_before)AND ph.contract_address IN (SELECT token_bought_mint_address FROM a_before)GROUP BY ph.contract_address, DATE_TRUNC('hour', ph.hour)),current_prices AS (SELECT a.token_bought_mint_address, a.day, ph.price AS current_priceFROM a_before aJOIN dex_solana.price_hour phON a.token_bought_mint_address = ph.contract_addressAND ph.hour = DATE_TRUNC('hour', a.day)),scam as (SELECT cp.token_bought_mint_address, cp.day, cp.current_price, pb.price_4h_beforeFROM current_prices cpJOIN price_4h_before pbON cp.token_bought_mint_address = pb.contract_addressAND pb.hour = DATE_TRUNC('hour', cp.day) - INTERVAL '4' hourWHERE cp.current_price <= 0.2 * pb.price_4h_before),a as (select * from a_beforewhere token_bought_mint_address not in (select token_bought_mint_address from scam)), b as (select avg(price) as price,date_trunc('hour',hour) as day,lower(contract_address) as contract_address,symbol from dex_solana.price_hourwhere lower(contract_address) in (select lower(token_bought_mint_address) from a)group by 2,3,4),TokenPurchase AS (SELECTa.token_bought_mint_address,a.day AS purchase_day,b.price AS bought_price,b.symbol,SUM(100 / b.price) AS total_quantity -- Sum quantities for multiple rowsFROMaJOINbONa.token_bought_mint_address = b.contract_addressWHERE