Query editor
WITH params AS (SELECTINTERVAL '{{interval}}' DAY AS lookback,casewhen {{interval}} > 7 then 'day'when {{interval}} > 150 then 'week'else 'hour'end AS granularity),blocks AS (SELECT'base' AS chain,date_trunc(p.granularity, b.time) AS bucket,approx_percentile(base_fee_per_gas, 0.5) AS median_base_fee,approx_percentile(gas_used, 0.5) AS median_gas_used,approx_percentile(gas_used * 1.0 / (60*1e6), 0.5) AS median_gas_utilFROM base.blocks bCROSS JOIN params pWHERE b.time >= now() - p.lookbackGROUP BY 1, 2),txs AS (SELECT'base' AS chain,date_trunc(p.granularity, t.block_time) AS bucket,approx_percentile(gas_price, 0.5) AS median_gas_price,approx_percentile(priority_fee_per_gas, 0.5) AS median_priority_fee,MIN(gas_price) AS min_gas_price,MIN_BY(hash, gas_price) AS tx_with_min_gas_price,MAX(gas_price) AS max_gas_price,MAX_BY(hash, gas_price) AS tx_with_max_gas_priceFROM base.transactions tCROSS JOIN params pWHERE t.block_time >= now() - p.lookback AND gas_price > 0GROUP BY 1, 2),combined AS (SELECTCOALESCE(b.chain, t.chain) as chain,bucket AS bucket,b.median_gas_used,b.median_gas_util,b.median_base_fee,t.median_gas_price,t.median_priority_fee,t.median_gas_price / b.median_base_fee as gas_price_ratio,t.median_priority_fee * 1.0 / b.median_base_fee as priority_fee_ratio,t.min_gas_price,t.tx_with_min_gas_price,t.max_gas_price,t.tx_with_max_gas_priceFROM blocks bFULL OUTER JOIN txs t USING (bucket))SELECT * FROM combined