Query editor
-- 1. Get First Transaction Date for each user (for New/Returning logic)WITH user_first_tx AS (SELECT"from" AS user_address,MIN(t.block_date) AS first_tx_dateFROMplasma.transactions tGROUP BY 1),-- 2. Calculate Daily Net Flow for Wallet Balances (for Top Holders)wallet_net_flow AS (SELECTa.user_address,-- Treat all 'from' as outflows and all 'to' as inflowsCAST(SUM(CASE WHEN t."from" = a.user_address THEN -t.value/1e18 ELSE t.value/1e18 END)AS DECIMAL(38, 4)) AS current_token_balanceFROMplasma.transactions t-- Get all unique addresses involved as senders or recipientsCROSS JOIN (SELECT "from" AS user_address FROM plasma.transactionsUNIONSELECT "to" AS user_address FROM plasma.transactions) a-- Filter out the protocol address from the wallet balance list, but keep it in the flow calculationWHERE a.user_address != 0x405FBc9004D857903bFD6b3357792D71a50726b0GROUP BY 1),-- 3. Identify Top 100 Holders (CTE kept for documentation/potential separate query)top_holders AS (SELECTuser_address,current_token_balance,RANK() OVER (ORDER BY current_token_balance DESC) AS holder_rankFROMwallet_net_flowWHEREcurrent_token_balance > 0LIMIT 100),-- 4. Categorize Transactions and Determine User Status (Combined CTE)categorized_transactions AS (SELECTt.block_date,t."from" AS user_address,uft.first_tx_date, -- Join the first transaction date hereCAST(t.value/1e18 AS DECIMAL(38, 0)) AS transaction_value,-- Categorize the transaction to define Deposit/WithdrawalCASEWHEN t."to" = 0x405FBc9004D857903bFD6b3357792D71a50726b0 AND t.value > 0 THEN 'Deposit'WHEN t."from" = 0x405FBc9004D857903bFD6b3357792D71a50726b0 AND t.value > 0 THEN 'Withdrawal'ELSE 'Other'END AS transaction_typeFROM