Query editor
WITH-- Get all events that affect balances with proper signsall_events AS (-- Regular transfers: negative for sender, positive for receiverSELECTblock_time,block_number,tx_hash,"from" AS address,-(amount) AS amount_changeFROM tokens_sonic.transferswhere contract_address = 0x3bcE5CB273F0F148010BbEa2470e7b5df84C7812UNION ALLSELECTblock_time,block_number,tx_hash,"to" AS address,amount AS amount_changeFROM tokens_sonic.transferswhere contract_address = 0x3bcE5CB273F0F148010BbEa2470e7b5df84C7812),--Get current balances (like your Query 2)current_holders AS (SELECTaddress,SUM(amount_change) AS current_balanceFROM all_eventsWHERE address IS NOT NULLAND address != 0x0000000000000000000000000000000000000000GROUP BY addressHAVING SUM(amount_change) > 0.0000000001),-- Calculate daily cumulative balances only for current holdersdaily_balances_current_holders AS (SELECTdate_trunc('day', ae.block_time) AS day,ae.address,SUM(ae.amount_change) AS daily_net_change,SUM(SUM(ae.amount_change)) OVER (PARTITION BY ae.addressORDER BY date_trunc('day', ae.block_time)ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_balanceFROM all_events aeINNER JOIN current_holders ch ON ae.address = ch.addressWHERE ae.address IS NOT NULLAND ae.address != 0x0000000000000000000000000000000000000000GROUP BY 1, 2),-- Get all dates that exist in the datadate_series AS (SELECT DISTINCT dayFROM daily_balances_current_holdersORDER BY day