Query editor
WITHcumulative_balances AS (SELECTuser,'curve-msteth/steth' as token,dt as evt_block_time,msteth_balance AS balanceFROM query_3572675union allSELECTuser,'curve-msweth/rsweth' as token,dt as evt_block_time,msteth_balance AS balanceFROM query_4270635union allSELECTuser,'curve-msweth/sweth(depreciated)' as token,dt as evt_block_time,msweth_balance AS balanceFROM query_3579769),holding_durations_with_lead AS (SELECTuser,token,evt_block_time AS start_time,-- Explicitly ordering the data to ensure consistency in the LEAD functionLEAD(evt_block_time) OVER (PARTITION BY user, tokenORDER BY evt_block_time ASC -- Ensure chronological order) AS end_time,balanceFROM cumulative_balances),holding_durations AS (SELECTuser,token,GREATEST(start_time, TRY_CAST('{{start_time}}' AS TIMESTAMP)) AS start_time,LEAST(COALESCE(end_time, TRY_CAST('{{end_time}}' AS TIMESTAMP)), TRY_CAST('{{end_time}}' AS TIMESTAMP)) AS end_time,balanceFROM holding_durations_with_leadWHEREstart_time < TRY_CAST('{{end_time}}' AS TIMESTAMP)AND COALESCE(end_time, TRY_CAST('{{end_time}}' AS TIMESTAMP)) > TRY_CAST('{{start_time}}' AS TIMESTAMP)),hourly_points AS (SELECTuser,token,date_diff('hour', start_time, end_time) AS hours_held,balanceFROM holding_durationsWHERE balance > 0 AND start_time < end_time)SELECTuser,token as symbol,