Query editor
WITHbnb_pool AS (SELECT DISTINCTCONCAT(token_0.symbol, ' - ', token_1.symbol) AS pair,poolpair.*FROMopenleverage_bnb.ControllerV1_evt_LPoolPairCreated AS poolpairJOIN tokens.erc20 AS token_0 ON poolpair.token0 = token_0.contract_addressJOIN tokens.erc20 AS token_1 ON poolpair.token1 = token_1.contract_addressWHEREtoken_0.blockchain = 'bnb'AND token_1.blockchain = 'bnb'),eth_pool AS (SELECT DISTINCTCONCAT(token_0.symbol, ' - ', token_1.symbol) AS pair,poolpair.*FROMopenleverage_ethereum.ControllerDelegator_evt_LPoolPairCreated AS poolpairJOIN tokens.erc20 AS token_0 ON poolpair.token0 = token_0.contract_addressJOIN tokens.erc20 AS token_1 ON poolpair.token1 = token_1.contract_addressWHEREtoken_0.blockchain = 'ethereum'AND token_1.blockchain = 'ethereum'),arb_pool AS (SELECT DISTINCTCONCAT(token_0.symbol, ' - ', token_1.symbol) AS pair,poolpair.*FROMopenleverage_arbitrum.ControllerDelegator_evt_LPoolPairCreated AS poolpairJOIN tokens.erc20 AS token_0 ON poolpair.token0 = token_0.contract_addressJOIN tokens.erc20 AS token_1 ON poolpair.token1 = token_1.contract_addressWHEREtoken_0.blockchain = 'arbitrum'AND token_1.blockchain = 'arbitrum'),MarginTrade AS (SELECTMarginTrade.evt_block_time,MarginTrade.evt_tx_hash,MarginTrade.marketId,pair,trader,held,MarginTrade.fees AS open_fees,CASEWHEN depositToken = TRUE THEN token1ELSE token0END AS token_addr,longTokenFROMbnb_poolLEFT JOIN openleverage_bnb.OpenLevV1_evt_MarginTrade AS MarginTrade ON MarginTrade.marketId = bnb_pool.marketIdWHEREMarginTrade.evt_block_time >= CAST('2022-01-17' AS TIMESTAMP)-- AND bnb_pool.marketId = 14),CloseTrade AS (SELECT