Query editor
-- Lookup Tables: Prices (hourly)-- One row per (blockchain, token, hour). Rule: mid price = (min_price + max_price) / 2.-- mid_price = last mid price of the hour (close); hours with no update carry the last known price forward.WITHupdates AS (SELECTblockchain, token, DATE_TRUNC('hour', block_time) AS period, block_time, block_number * 100000 + index AS event_order, (min_price + max_price) / 2 AS mid_priceFROM gmx_v2.oracle_price_updateWHERE min_price > 0AND max_price > 0-- AND block_time >= CURRENT_TIMESTAMP - INTERVAL '7' DAY -- test window), period_agg AS (SELECTblockchain, token, period, MAX_BY(mid_price, event_order) AS close_mid_price, AVG(mid_price) AS avg_mid_price, MIN(mid_price) AS low_mid_price, MAX(mid_price) AS high_mid_price, COUNT(*) AS update_count, MAX(block_time) AS last_update_timeFROM updatesGROUP BY 1, 2, 3), token_range AS (SELECTblockchain, token, MIN(period) AS first_periodFROM period_aggGROUP BY 1, 2), grid AS (SELECTtr.blockchain, tr.token, h.timestamp AS periodFROM token_range AS trINNER JOIN utils.hours AS hON h.timestamp >= tr.first_periodAND h.timestamp <= DATE_TRUNC('hour', CURRENT_TIMESTAMP)), filled AS (SELECTg.blockchain, g.token, g.period, pa.update_count, pa.avg_mid_price, pa.low_mid_price, pa.high_mid_price, LAST_VALUE(pa.close_mid_price) IGNORE NULLS OVER w AS mid_price, LAST_VALUE(pa.last_update_time) IGNORE NULLS OVER w AS last_update_time