Query editor
WITH transfers AS (-- Step 1: Get all transfers for the token (both inflows and outflows)SELECT"from" AS address,-CAST(value AS DOUBLE) / 1e18 AS amount, -- Outflows are negativeevt_block_timeFROM erc20_ethereum.evt_TransferWHERE contract_address = 0x57e114b691db790c35207b2e685d4a43181e6061UNION ALLSELECT"to" AS address,CAST(value AS DOUBLE) / 1e18 AS amount, -- Inflows are positiveevt_block_timeFROM erc20_ethereum.evt_TransferWHERE contract_address = 0x57e114b691db790c35207b2e685d4a43181e6061),balance_calculation AS (-- Step 2: Calculate current and historical balances for ALL holdersSELECTaddress,SUM(amount) AS current_balance,SUM(CASE WHEN evt_block_time < (NOW() - INTERVAL '2' DAY) THEN amount ELSE 0 END) AS balance_2d_ago,SUM(CASE WHEN evt_block_time < (NOW() - INTERVAL '7' DAY) THEN amount ELSE 0 END) AS balance_7d_ago,SUM(CASE WHEN evt_block_time < (NOW() - INTERVAL '14' DAY) THEN amount ELSE 0 END) AS balance_14d_ago,SUM(CASE WHEN evt_block_time < (NOW() - INTERVAL '42' DAY) THEN amount ELSE 0 END) AS balance_42d_agoFROM transfersGROUP BY address),final_results AS (-- Step 3: Calculate the changes for large holdersSELECTb.address,l.name AS ens_name,b.current_balance,(b.current_balance - b.balance_2d_ago) AS change_last_2d,(b.current_balance - b.balance_7d_ago) AS change_last_7d,(b.current_balance - b.balance_14d_ago) AS change_last_14d,(b.current_balance - b.balance_42d_ago) AS change_last_42dFROM balance_calculation bLEFT JOIN labels.ens l ON b.address = l.address--WHERE b.current_ > 20000 -- Filter for holders with more than 500,000 tokens)-- Step 4: Filter out inactive holders and sortSELECT *FROM final_resultsWHERE change_last_2d <> 0 OR change_last_7d <> 0 OR change_last_14d <> 0 OR change_last_42d <> 0 -- This is the new filterORDER BY change_last_42d DESC -- Sort by the 6-hour change, highest outflow first