Query editor
-- Params:-- {{time_granularity}} -- manual list: day|week|month-- {{lookback_periods}} -- integer: number of periods (e.g., 12 months)WITHparams AS (SELECTCASE LOWER('{{time_granularity}}')WHEN 'week' THEN 'week'WHEN 'month' THEN 'month'ELSE 'day'END AS gran,COALESCE(TRY_CAST('{{lookback_periods}}' AS integer), 90) AS periods),window AS (SELECTgran,CASE granWHEN 'month' THEN date_add('month', -periods, now())WHEN 'week' THEN date_add('week', -periods, now())ELSE date_add('day', -periods, now())END AS start_tsFROM params),-- One label per address (latest) for Baselabels_base AS (SELECT address, name, category, label_typeFROM (SELECTla.address,la.name,la.category,la.label_type,la.updated_at,ROW_NUMBER() OVER (PARTITION BY la.address ORDER BY la.updated_at DESC) AS rnFROM labels.addresses laWHERE la.blockchain = 'base')WHERE rn = 1),/* -------- Stage 1: find top contracts by leaf-trace gas in window -------- */top_contracts AS (SELECTt."to" AS callee, -- "to" is a keyword; quoting is OK in TrinoSUM(t.gas_used) AS gas_used_leaf_sumFROM base.traces tCROSS JOIN windowWHERE t.block_time >= window.start_tsAND t.type = 'call'AND t.sub_traces = 0 -- leaf frames only to avoid double-countingAND t.success = TRUEGROUP BY 1ORDER BY gas_used_leaf_sum DESCLIMIT 500 -- tune for breadth vs. speed),/* -------- Stage 2a: per-bucket gas for the top contracts -------- */per_bucket_contract_gas AS (