Query editor
WITH daily_transactions AS (-- Step 1: Collect all transactions, tagged with day and their effect on balancesSELECT"from" AS address,DATE(evt_block_time) AS day,-value / 1e18 AS amtFROMerc20_sonic.evt_transferWHEREevt_block_time > CAST('2024-12-05' AS TIMESTAMP)AND contract_address = 0x79bbF4508B1391af3A0F4B30bb5FC4aa9ab0E07CUNION ALLSELECT"to" AS address,DATE(evt_block_time) AS day,value / 1e18 AS amtFROMerc20_sonic.evt_transferWHEREevt_block_time > CAST('2024-12-05' AS TIMESTAMP)AND contract_address = 0x79bbF4508B1391af3A0F4B30bb5FC4aa9ab0E07C),daily_balances AS (-- Step 2: Aggregate daily changes for each addressSELECTaddress,day,SUM(amt) AS daily_changeFROM daily_transactionsGROUP BY address, day),cumulative_balances AS (-- Step 3: Compute cumulative balances for each address, for each daySELECTaddress,day,SUM(daily_change) OVER (PARTITION BY address ORDER BY day ROWS UNBOUNDED PRECEDING) AS cumulative_balanceFROM daily_balances),distinct_days AS (-- Step 4: Ensure all days are covered, even if there are no transactionsSELECT DISTINCT dayFROM daily_balances),holders_per_day AS (-- Step 5: For each day, count addresses with a positive balanceSELECTd.day,COUNT(DISTINCT cb.address) AS holder_countFROM distinct_days dLEFT JOIN cumulative_balances cbON d.day = cb.day AND cb.cumulative_balance > 0GROUP BY d.day)SELECTday,holder_countFROM holders_per_day