Query editor
WITH asset_trades AS (SELECTtrader_id,SUM(CASEWHEN token_sold_symbol != 'SOL' AND token_bought_symbol = 'SOL' THEN amount_usdELSE 0END) AS total_sell_usd,SUM(CASEWHEN token_sold_symbol = 'SOL' AND token_bought_symbol != 'SOL' THEN amount_usdELSE 0END) AS total_buy_usdFROM dex_solana.tradesWHERE project_main_id = '6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P'AND block_time < CAST(current_date AS TIMESTAMP)GROUP BY trader_id),trader_pnl AS (SELECTtrader_id,total_buy_usd,total_sell_usd,(total_sell_usd - total_buy_usd) AS pnlFROM asset_trades),-- Focus on wallets that actually made a profitwinners AS (SELECT *FROM trader_pnlWHERE pnl > 0),-- Rank winners by their pnl in descending order and get total count of winnersranked_winners AS (SELECTtrader_id,pnl,ROW_NUMBER() OVER (ORDER BY pnl DESC) AS rank,COUNT(*) OVER () AS total_winnersFROM winners),-- Select the top 1% of winnerstop_percent AS (SELECTtrader_id,pnlFROM ranked_winnersWHERE rank <= total_winners * 0.01),-- Sum the total profit of all winning walletstotal_winner_profit AS (SELECT SUM(pnl) AS total_profit_all FROM winners),-- Sum the profit captured by the top 1% walletstop_profit_sum AS (SELECT SUM(pnl) AS top_profit FROM top_percent)SELECTtop_profit,total_profit_all,(top_profit/total_profit_all)*100 AS top_1_percent_profit_shareFROM top_profit_sum, total_winner_profit;