Query editor
-- SPX PnLwith spx_price as(select hour, median_price as pricefrom dex.priceswhere contract_address = 0xE0f63A424a4439cBE457D80E4f4b51aD25b2c56C --SPX), spx_last_price as(select pricefrom spx_priceorder by hour desclimit 1), spx_trades as(select '+ buy' as trade_type, tx_hash, tx_from as account, block_time, token_bought_amount * (-1) as amount, token_bought_amount * price * (-1) as usdfrom dex.trades trdjoin spx_price prc on date_trunc('hour', trd.block_time) = prc.hourwhere token_bought_address = 0xE0f63A424a4439cBE457D80E4f4b51aD25b2c56C --SPX-- and tx_from = 0x000000000000000000000000000000000000dEaDunion allselect '- sell' as trade_type, tx_hash, tx_from as account, block_time, token_sold_amount as amount, token_sold_amount * price as usdfrom dex.trades trdjoin spx_price prc on date_trunc('hour', trd.block_time) = prc.hourwhere token_sold_address = 0xE0f63A424a4439cBE457D80E4f4b51aD25b2c56C --SPX-- and tx_from = 0x000000000000000000000000000000000000dEaD), summary as(select account, sum(usd) as net_usd, sum(amount) as net_amountfrom spx_tradesgroup by 1), summary_pnl as(select account, net_usd, abs(net_amount) as hold_amount, (select price from spx_last_price) * power(10, 9) as last_price_nusd, case when net_amount < 0 then (-1) * net_amount * (select price from spx_last_price) else 0 end as hold_profit_amount, net_usd + case when net_amount < 0 then (-1) * net_amount * (select price from spx_last_price) else 0 end as pnl_usdfrom summary--order by pnl_usd desc)select case when pnl_usd >= 0 then 'Profiter' else 'Loser' end as pnl_type, count(account) as count_accfrom summary_pnlgroup by 1