Query editor
WITH-- we select only the staking and unstaking events (ignore claims).staking_events AS (SELECTaddress,block_time as event_time,ilv_delta,slp_delta,-- give each event for an address a sequence index.-- we use event_id because we need one column that defines the event sequence, so we can select max(event_id) later on.row_number() OVER (PARTITION BY address ORDER BY CAST(block_time as date), index) AS event_idFROMquery_2667500WHEREis_claim = FALSE),-- get a list of unique staker addressesstakers AS (SELECT DISTINCTaddressFROMstaking_events),-- get a list of timestamps we want a number of active stakers for.time_seq AS (SELECT range.time timeFROM unnest(sequence(cast(cast('{{Date from}}' AS timestamp) as date), cast(current_timestamp as date), (interval '1' day))) as range(time)),-- calculate the total accumulated balance for each address on each time.accumulated_balances AS (SELECTaddress,event_time, -- we keep the event timeevent_id, -- this is the actual event sequence id, multiple events may exist with the same event time.SUM(ilv_delta) OVER (PARTITION BY address ORDER BY event_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS ilv_value,SUM(slp_delta) OVER (PARTITION BY address ORDER BY event_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS slp_valueFROMstaking_events),-- finds the last staking event for each address, for the timestamps we are interested in.time_last_event AS (SELECTw.time time,c.address address,max(d.event_id) last_event_idFROMtime_seq wCROSS JOINstakers cLEFT JOINaccumulated_balances d ON d.event_time < (w.time + (interval '1' day)) AND c.address = d.addressGROUP BYw.time, c.address),time_last_balance AS (SELECTt.time,t.address,e.ilv_value as last_ilv_value,e.slp_value as last_slp_value