Query editor
WITH avi_trades AS (SELECTblock_time,amount_usd,CASEWHEN token_bought_address = 0xd2bdaaf2b9cc6981fd273dcb7c04023bfbe0a7feTHEN amount_usd / token_bought_amountWHEN token_sold_address = 0xd2bdaaf2b9cc6981fd273dcb7c04023bfbe0a7feTHEN amount_usd / token_sold_amountEND AS price_usdFROM dex.tradesWHERE blockchain = 'ethereum'AND (token_bought_address = 0xd2bdaaf2b9cc6981fd273dcb7c04023bfbe0a7feOR token_sold_address = 0xd2bdaaf2b9cc6981fd273dcb7c04023bfbe0a7fe)AND amount_usd > 0),-- Filter to "normal" trades and pricescleaned AS (SELECTdate_trunc('day', block_time) AS day,price_usdFROM avi_tradesWHERE price_usd IS NOT NULLAND price_usd > 0AND price_usd < 0.05 -- drop insane spikes; adjust this cap if neededAND amount_usd BETWEEN 50 AND 50000 -- ignore dust + weird micro trades),daily_prices AS (SELECTday,approx_percentile(price_usd, 0.5) AS median_price_usdFROM cleanedGROUP BY 1)SELECTday,median_price_usd AS avg_price_usd -- keep the same column name for the chartFROM daily_pricesORDER BY day;