Query editor
WITH token_trades AS (SELECTtrader_id,CASEWHEN token_sold_symbol IN ('SOL','WSOL') THEN token_bought_mint_address -- Buying tokenWHEN token_bought_symbol IN ('SOL','WSOL') THEN token_sold_mint_address -- Selling tokenELSE NULLEND AS token_address,CASEWHEN token_sold_symbol IN ('SOL','WSOL') THEN -amount_usd -- Buying (negative = money spent)WHEN token_bought_symbol IN ('SOL','WSOL') THEN amount_usd -- Selling (positive = money received)ELSE 0END AS net_amount,CASEWHEN token_sold_symbol IN ('SOL','WSOL') THEN amount_usd -- Buy amountELSE 0END AS buy_amount,CASEWHEN token_bought_symbol IN ('SOL','WSOL') THEN amount_usd -- Sell amountELSE 0END AS sell_amount,block_timeFROM dex_solana.tradesWHERE block_time > TIMESTAMP '2025-08-05'AND (token_sold_symbol IN ('SOL','WSOL') OR token_bought_symbol IN ('SOL','WSOL'))),token_profits AS (SELECTtrader_id,token_address,COUNT(*) AS token_transactions,SUM(net_amount) AS token_profit,SUM(buy_amount) AS total_bought,SUM(sell_amount) AS total_sold,COUNT(CASE WHEN buy_amount > 0 THEN 1 END) AS buy_count,COUNT(CASE WHEN sell_amount > 0 THEN 1 END) AS sell_countFROM token_tradesWHERE token_address IS NOT NULLGROUP BY trader_id, token_address),trader_summary AS (SELECTtp.trader_id,COUNT(DISTINCT tp.token_address) AS unique_tokens_traded,SUM(tp.token_transactions) AS total_transactions,SUM(tp.token_profit) AS total_profit,AVG(tp.total_bought / NULLIF(tp.buy_count, 0)) AS avg_buy_size,SUM(CASE WHEN tp.token_profit > 0 THEN 1 ELSE 0 END) AS profitable_tokens,COUNT(*) AS total_token_positions,MAX(tt.block_time) AS last_transactionFROM token_profits tpJOIN token_trades tt ON tp.trader_id = tt.trader_idGROUP BY tp.trader_idHAVING SUM(tp.token_transactions) BETWEEN 51 AND 999