Query editor
-- ============================================================================-- Query 4: Monthly Aggregation-- ============================================================================WITHlock_events AS (SELECTevt_block_number AS block_number, evt_index AS index, evt_block_time AS block_time, user, CAST(amount AS DOUBLE) / 1e18 AS pendle_amount, FROM_UNIXTIME(CAST(expiry AS BIGINT)) AS expiry_timestamp, DATE_TRUNC('day', FROM_UNIXTIME(CAST(expiry AS BIGINT))) AS expiry_dayFROM pendle_ethereum.VotingEscrowPendleMainchain_evt_NewLockPosition), withdrawal_events AS (SELECT DISTINCTuser, MAX(evt_block_time) AS last_withdrawal_time -- FIXEDFROM pendle_ethereum.VotingEscrowPendleMainchain_evt_WithdrawGROUP BY user), latest_user_positions AS (SELECTuser, pendle_amount, expiry_timestamp, expiry_day, block_time AS last_updatedFROM (SELECTuser, pendle_amount, expiry_timestamp, expiry_day, block_time, block_number, index, ROW_NUMBER() OVER (PARTITION BY userORDER BY block_number DESC, index DESC) AS rnFROM lock_events) rankedWHERE rn = 1), active_positions AS (SELECTlp.user, lp.pendle_amount, lp.expiry_day, DATE_DIFF('day', CAST(NOW() AS TIMESTAMP), lp.expiry_timestamp) AS days_until_unlockFROM latest_user_positions lpLEFT JOIN withdrawal_events wuON lp.user = wu.user