Query editor
WITHstaking_unstaking as (SELECT -- depositsdate_trunc('day', call_block_time) as day,subject,SUM(stakeValue/1e18) as stakeFROMforta_network_polygon.FortaStaking_call_deposit fINNER JOINpolygon.transactions pON f.call_tx_hash = p.hash-- AND f.subjectType = 1WHERE p.block_time >= CAST('2022-03-08' as timestamp)AND p.to = 0xd2863157539b1D11F39ce23fC4834B62082F6874AND p.hash != 0x4132d43661e04b69641f2d526d5e0086c280dfe9a454f8386017b1e15b0efaf9AND bytearray_substring(p.data,1,4) = 0x2cb31144AND f.call_success = trueGROUP BY 1, 2UNION ALLSELECT -- withdrawalsdate_trunc('day', call_block_time) as day,subject,-1 * SUM(output_0/1e18) as stakeFROMforta_network_polygon.FortaStaking_call_withdraw fINNER JOINpolygon.transactions pON f.call_tx_hash = p.hash-- AND f.subjectType = 1WHERE p.block_time >= CAST('2022-03-08' as timestamp)AND p.to = 0xd2863157539b1D11F39ce23fC4834B62082F6874AND bytearray_substring(p.data,1,4) = 0x3f489914AND f.call_success = trueGROUP BY 1, 2),balances as (SELECTday,subject,SUM(stake) as stakeFROMstaking_unstakingGROUP BY 1, 2),rolling_balance as (SELECTday,subject,SUM(stake) OVER (PARTITION BY subject ORDER BY day ASC) as stake_over_time,lead(DAY, 1, now() ) OVER (PARTITION BY subject ORDER BY day asc) as next_dayFROMbalances),