Query editor
WITHtransferEvents AS(SELECTcontract_address,evt_block_number,evt_block_time,evt_index,evt_tx_hash,"from","to",valueFROMerc20_{{blockchain}}.evt_TransferWHEREevt_block_number >= {{initial_block_number}}AND contract_address = {{token_address}}),firstTransfers AS (SELECT"to" AS address,MIN(evt_block_time) AS first_transfer_timeFROMtransferEventsGROUP BY"to"),initialBalances AS (SELECTt."to" AS address,SUM(CAST(t.value AS INT256)) AS initial_balance,f.first_transfer_time AS initial_buy_timeFROMtransferEvents t JOIN firstTransfers f ON t."to"=f.address AND t.evt_block_time = f.first_transfer_timeGROUP BY t."to", f.first_transfer_time),currentTransfers AS ((SELECT"to" AS address,SUM(CAST(value AS INT256)) AS balance --positive changes in the balance of the walletFROM transferEventsGROUP BY "to")UNION ALL(SELECT"from" AS address,- SUM(CAST(value AS INT256)) AS balance --negative changes in the balance of the walletFROM transferEventsWHERE "from" != 0x0000000000000000000000000000000000000000GROUP BY "from")),currentBalances AS (SELECTaddress,SUM(balance)/{{decimals}} AS balanceFROM currentTransfersGROUP BY addressHAVING SUM(balance) > CAST(0 AS INT256) --This is to ensure that there are no negative balance