Query editor
WITH Total_Contracts_Traded AS (SELECTtrader,COUNT(DISTINCT contract_address) AS total_contracts_traded -- Total contracts traded by each traderFROM dune.brightspear.result_solana_general_no_contractGROUP BY trader),-- Subquery to calculate the count of positive profits per wallet (trader)Profit_Count AS (SELECTtrader,COUNT(DISTINCT contract_address) AS profitable_contracts_countFROM (SELECTtrader,contract_address,SUM(profit) AS total_profit_per_contractFROM dune.brightspear.result_solana_general_no_contractWHERE profit > 0GROUP BY trader, contract_address) AS filtered_profitsGROUP BY trader),Profit_Count_buyer AS (SELECTtrader,COUNT(DISTINCT contract_address) AS profitable_count_buyerFROM (SELECTtrader,contract_address,SUM(profit) AS total_profit_per_contractFROM dune.brightspear.result_solana_general_no_contractWHERE profit > 0AND label1 = 'buyer'AND token_amount_sold < token_amount_bought + token_amount_bought * 0.5GROUP BY trader, contract_address) AS filtered_profitsGROUP BY trader),-- Subquery to get the highest total profit contract per traderHighest_Profit_Contract AS (SELECTtrader,contract_address,SUM(profit) AS total_profit_per_contract,label1, -- Retrieve labelslabel2,label3,ROW_NUMBER() OVER (PARTITION BY trader ORDER BY SUM(profit) DESC) AS rnFROM dune.brightspear.result_solana_general_no_contractWHERE profit > 0 -- Only consider profitable contractsGROUP BY trader, contract_address, label1, label2, label3),PNL AS (SELECT