Query editor
with account_connection as (select distinct * from(select primaryAccount as account , subAccount from hmx_arbitrum.TradeService_evt_LogIncreasePosition)),pnl as (select ac.account as account , sum(coalesce(pnl.pnl,0))/1e30 as pnl from account_connection acleft join(select subAccount , sum(pnl) as pnl from((select subAccount, -sum(cast(usd as double)) as pnl from hmx_arbitrum.TradeHelper_evt_LogSettleUnRealizedPnlValuegroup by subAccount)union all(select subAccount, sum(cast(usd as double)) as pnl from hmx_arbitrum.TradeHelper_evt_LogReceivedUnRealizedPnlValuegroup by subAccount))group by subAccount) pnlon ac.subAccount = pnl.subAccountgroup by ac.account)select row_number() over (order by pnl.pnl desc) as "rank", ac.account, pnl.pnl as PnL from account_connection acleft join pnlon ac.account = pnl.accountorder by pnl desc