Query editor
WITH tx AS (SELECTday,SUM(amount) as amountFROM (SELECTdate_trunc('day', evt_block_time) AS day,SUM(loan_increase/1e18) AS amountFROM (SELECT * FROM curvefi_ethereum.crvusd_controller_5_evt_BorrowUNION ALLSELECT * FROM curvefi_ethereum.crvusd_controller_wsteth_evt_Borrow)GROUP BY 1UNION ALLSELECTdate_trunc('day', evt_block_time) AS day,-SUM(loan_decrease/1e18) AS amountFROM(SELECT * FROM curvefi_ethereum.crvusd_controller_wsteth_evt_RepayUNION ALLSELECT * FROM curvefi_ethereum.crvusd_controller_5_evt_Repay)GROUP BY 1) tGROUP BY day), balances_with_gap_days AS (SELECTday,SUM(amount) OVER (ORDER BY day ASC) AS amount -- balance per day with a transaction,LEAD(day, 1, now()) OVER (ORDER BY day asc) AS next_dayFROM tx), days AS(WITH days_seq AS (SELECTsequence((SELECT cast(min(date_trunc('day', day)) as timestamp) day FROM tx), date_trunc('day', cast(now() as timestamp)), interval '1' day) as day)SELECTdays.dayFROM days_seqCROSS JOIN unnest(day) as days(day) --this is just doing explode like in spark sql), balance_all_days AS (SELECTd.day,SUM(amount) AS amountFROM balances_with_gap_days bINNER JOIN days d ON b.day <= d.day