Query editor
WITH normalized_tokenregistered AS (SELECTconditionId,LEAST(token0, token1) AS token_min,GREATEST(token0, token1) AS token_max,ROW_NUMBER() OVER (PARTITION BY LEAST(token0, token1), GREATEST(token0, token1) ORDER BY conditionId) AS rnFROM probable_v1_bnb.ctfexchange_evt_tokenregistered),unique_tokenreg AS (SELECT conditionId, token_min, token_maxFROM normalized_tokenregisteredWHERE rn = 1),order_w_conditionId AS (SELECTa.*,cast(b.conditionId as VARCHAR) as conditionIdFROM probable_v1_bnb.ctfexchange_evt_ordersmatched aLEFT JOIN unique_tokenreg bON (CASE WHEN a.takerAssetId = 0 THEN a.makerAssetId ELSE a.takerAssetId END) IN (b.token_min, b.token_max)ORDER BY a.evt_block_date desc)select *,case when a.takerAssetId = 0 and cast(a.makerAssetId as varchar) = yes_token then 'YES'when a.takerAssetId = 0 and cast(a.makerAssetId as varchar)= no_token then 'NO'when a.makerAssetId = 0 and cast(a.takerAssetId as varchar) = yes_token then 'YES'when a.makerAssetId = 0 and cast(a.takerAssetId as varchar)= no_token then 'NO'end as outcomefrom order_w_conditionId aleft join query_6365413 b on b.conditionId = a.conditionId