Query editor
WITH latest_whales AS (SELECTDISTINCT address, blockchain, balance_native, balance_usdFROM dune.apechain.result_ape_whales_whales_listWHERE block_week = date_trunc('week', CURRENT_DATE - INTERVAL '7' DAY)),flows AS (-- incoming to whales (positive)SELECTdate_trunc('week', t.block_date) AS block_week,t."to" AS address,t.blockchain,t.contract_address,t.symbol,t.amount AS signed_amount--'in' AS flow,FROM tokens.transfers tJOIN latest_whales wON w.address = t."to" AND w.blockchain = t.blockchainUNION ALL-- outgoing from whales (negative)SELECTdate_trunc('week', t.block_date) AS block_week,t."from" AS address,t.blockchain,t.contract_address,t.symbol,-1*t.amount AS signed_amount--'out' AS flow,FROM tokens.transfers tJOIN latest_whales wON w.address = t."from" AND w.blockchain = t.blockchain),balances AS (SELECTsymbol,blockchain,contract_address,SUM(signed_amount) AS balanceFROM flowsWHERE contract_address NOT IN (0xB7b31a6BC18e48888545CE79e83E06003bE70930,0x4d224452801ACEd8B2F0aebE155379bb5D594381,0x7f9FBf9bDd3F4105C478b996B648FE6e828a1e98)GROUP BY 1,2,3),prices AS (SELECTdate_trunc('week', "timestamp") AS block_week,blockchain,contract_address,AVG(price) AS priceFROM prices.latestWHERE blockchain IN ('ethereum', 'polygon', 'arbitrum', 'apechain')AND date_trunc('week', "timestamp") = date_trunc('week', CURRENT_DATE)--AND volume >= 1e4 --Only get relevant token pricesAND symbol NOT IN ('HYPER','NeiroSPX','JIFFPOM','JUNFOX','GOONER') --These token are with incorrect prices... removing them here