Query editor
WITH calendar AS (SELECT DISTINCT DATE(evt_block_time) AS evt_block_dateFROM goatfi_sonic.ycusdce_evt_depositUNIONSELECT DISTINCT DATE(evt_block_time)FROM goatfi_sonic.ycusdce_evt_withdrawUNIONSELECT DISTINCT evt_block_dateFROM goatfi_sonic.ycusdce_evt_strategyreported),tx AS (SELECTDATE(evt_block_time) AS evt_block_date,SUM(assets) / 1e6 AS amountFROM goatfi_sonic.ycusdce_evt_depositGROUP BY DATE(evt_block_time)UNION ALLSELECTDATE(evt_block_time),-SUM(assets) / 1e6FROM goatfi_sonic.ycusdce_evt_withdrawGROUP BY DATE(evt_block_time)),daily_tx_aggregated AS (SELECTc.evt_block_date,COALESCE(SUM(GREATEST(tx.amount, 0)), 0) AS deposits,COALESCE(SUM(GREATEST(-tx.amount, 0)), 0) AS withdraws,COALESCE(SUM(tx.amount), 0) AS net_amountFROM calendar cLEFT JOIN tx ON c.evt_block_date = tx.evt_block_dateGROUP BY c.evt_block_date),daily_gains_losses AS (SELECTevt_block_date,SUM(gain) / 1e6 AS total_gain,-SUM(loss) / 1e6 AS total_lossFROM goatfi_sonic.ycusdce_evt_strategyreportedGROUP BY evt_block_date),cumulative_gains_losses AS (SELECTc.evt_block_date,COALESCE(dgl.total_gain, 0) AS total_gain,COALESCE(dgl.total_loss, 0) AS total_loss,SUM(COALESCE(dgl.total_gain, 0) - COALESCE(dgl.total_loss, 0)) OVER (ORDER BY c.evt_block_date) AS cumulative_totalFROM calendar cLEFT JOIN daily_gains_losses dgl ON c.evt_block_date = dgl.evt_block_date),final_aggregated AS (SELECTc.evt_block_date,