Query editor
WITHtrades AS (SELECTtrader,subject,evt_block_time AS block_time,evt_block_number AS block_number,IF(isBuy, 'Buy', 'Sell') AS type,shareAmount,IF(isBuy,CAST(shareAmount AS INT256),- CAST(shareAmount AS INT256)) AS shareAmountDelta,(amount / 1e18) / shareAmount AS sharePrice,amount / 1e18 AS volume,(protocolAmount + subjectAmount + referralAmount) / 1e18 AS fee,supply,evt_tx_hash AS tx_hashFROMstarsarena_avalanche_c.StarsArena_evt_Trade),traderLeaderboard AS (SELECTtrader,SUM(shareAmountDelta) AS currentBalance,SUM(IF(type = 'Buy', shareAmount, CAST(0 AS UINT256))) AS sharesBought,SUM(IF(type = 'Sell', shareAmount, CAST(0 AS UINT256))) AS sharesSold,SUM(volume) AS volume,SUM(fee) AS feesPaid,COUNT(DISTINCT (tx_hash)) AS numberOfTrades,MIN(block_time) AS firstTradeDate,MAX(block_time) AS latestTradeDate,SUM(IF(type = 'Buy', volume, 0)) AS totalBuyPrice,SUM(IF(type = 'Sell', volume - fee, 0)) AS totalSellPriceFROMtradesGROUP BYtrader)SELECTRANK() OVER (ORDER BYtotalSellPrice - totalBuyPrice - (numberOfTrades * 0.004) DESC) AS traderRank,traderLeaderboard.*,totalSellPrice - totalBuyPrice - (numberOfTrades * 0.004) as PnLFROMtraderLeaderboardGROUP BYtrader,currentBalance,sharesBought,sharesSold,volume,feesPaid,numberOfTrades,firstTradeDate,latestTradeDate,totalBuyPrice,