Query editor
-- This optimized version filters by wallet first to reduce the dataset size,-- then calculates the daily and cumulative USD value in fewer steps.WITH-- Step 1: Select and filter reward events for the specific wallet first.-- This is the most important optimization.rewards_filtered AS (SELECTevt_block_time,reward AS reward_token_address,amount AS reward_amountFROM aerodrome_base.VotingReward_evt_ClaimRewardsWHERE"from" = 0x7Cb552152e2b28F9f6911c51B69B0d8D1FADafe1 -- {{wallet}}AND evt_block_time >= (-- Calculate the start of the 10th previous Thursday-based epoch.-- This finds the most recent Thursday and subtracts 9 weeks.DATE_TRUNC('week',NOW() - INTERVAL '3' DAY) + INTERVAL '3' DAY - INTERVAL '63' day)),-- Step 2: Calculate the daily USD value by joining the filtered rewards-- with price and token data, then aggregating by day.daily_summary AS (SELECTDATE_TRUNC('day', r.evt_block_time) AS time,SUM((r.reward_amount / POWER(10, COALESCE(t.decimals, 18))) *COALESCE(p.price, 0)) AS daily_usd_valueFROM rewards_filtered AS r--LEFT JOIN prices.minute AS p ON p.blockchain = 'base' AND p.timestamp = DATE_TRUNC('minute', r.evt_block_time) AND p.contract_address = r.reward_token_address -- AND p.timestamp = DATE_TRUNC('minute', r.evt_block_time)LEFT JOIN prices.usd AS p ON p.blockchain = 'base' AND p.minute = DATE_TRUNC('minute', r.evt_block_time) AND p.contract_address = r.reward_token_address -- AND p.timestamp = DATE_TRUNC('minute', r.evt_block_time)LEFT JOIN tokens.erc20 AS t ON t.blockchain = 'base' AND t.contract_address = r.reward_token_address GROUP BY 1 -- Group by time)-- Step 3: Calculate the cumulative value using a window function.SELECTtime,daily_usd_value,SUM(daily_usd_value) OVER (ORDER BY time ASC) AS cumulative_usd_valueFROM daily_summaryORDER BY time ASC