Query editor
WITH token_transactions AS (SELECTtaker,SUM(CASE WHEN token_bought_address = 0x6c2c06790b3e3e3c38e12ee22f8183b37a13ee55 THEN token_bought_amount ELSE 0 END) AS tokensBought,SUM(CASE WHEN token_bought_address = 0x6c2c06790b3e3e3c38e12ee22f8183b37a13ee55 THEN amount_usd ELSE 0 END) AS boughtUsd,SUM(CASE WHEN token_sold_address = 0x6c2c06790b3e3e3c38e12ee22f8183b37a13ee55 THEN token_sold_amount ELSE 0 END) AS tokensSold,SUM(CASE WHEN token_sold_address = 0x6c2c06790b3e3e3c38e12ee22f8183b37a13ee55 THEN amount_usd ELSE 0 END) AS soldUsdFROM dex.tradesWHERE blockchain = 'arbitrum'AND (token_bought_address = 0x6c2c06790b3e3e3c38e12ee22f8183b37a13ee55OR token_sold_address = 0x6c2c06790b3e3e3c38e12ee22f8183b37a13ee55)GROUP BY taker),final AS (SELECTtaker AS user,ROUND(tokensBought - tokensSold, 4) AS holdings,ROUND(boughtUsd, 4) AS usdSpent,ROUND(boughtUsd / tokensBought, 4) AS avgPrice,ROUND(IF(soldUsd = 0, 0, boughtUsd - soldUsd), 4) AS pnl,ROUND(boughtUsd - (tokensSold * (boughtUsd / (tokensBought + tokensSold))), 4) AS net_usd_spentFROM token_transactions)--,totalLoss AS--(SELECTuser,holdings,usdSpent,avgPrice,pnl,net_usd_spent,CASEWHEN (holdings * 246.07) > net_usd_spent THEN 'Profit'ELSE 'Loss'END AS profit_or_loss_categoryFROM finalWHERE holdings > 0.1ORDER BY avgPrice DESC--)--SELECT--(SUM(holdings) * 246.07) - SUM(net_usd_spent) AS lossIncurred--FROM totalLoss