Query editor
/*WITHranked_transfers AS (SELECT*,ROW_NUMBER() OVER (PARTITION BY tx_id, outer_instruction_index, symbolORDER BY inner_instruction_index) AS symbol_occurrenceFROM tokens_solana.transfersWHEREdate_trunc('day', block_time) = DATE '2025-11-06'AND tx_id IN (SELECT tx_id FROM trade_directions)AND from_owner != to_owner),cycle_candidates AS (-- Find all cases where the same symbol returns later to its originSELECTt1.tx_id,t1.symbol,t1.outer_instruction_index,t1.inner_instruction_index AS start_inner_index,t2.inner_instruction_index AS end_inner_index,t1.amount_usd AS entry_amount_usd,t2.amount_usd AS exit_amount_usd,(t2.amount_usd - t1.amount_usd) AS profit_usd,t1.from_owner AS entry_from_owner,t1.to_owner AS entry_to_owner,t2.from_owner AS exit_from_owner,t2.to_owner AS exit_to_ownerFROM ranked_transfers t1INNER JOIN ranked_transfers t2ON t1.tx_id = t2.tx_idAND t1.outer_instruction_index = t2.outer_instruction_indexAND t1.symbol = t2.symbolAND t1.symbol_occurrence = 1AND t2.inner_instruction_index > t1.inner_instruction_indexAND t2.to_owner = t1.from_owner -- must return to same origin),first_cycle AS (-- Aggregate: pick the earliest end_inner_index per token per tx/outer_instruction comboSELECTtx_id,symbol,outer_instruction_index,MIN(end_inner_index) AS first_end_inner_indexFROM cycle_candidatesGROUP BY tx_id, symbol, outer_instruction_index),resolved_cycles AS (-- Join back to capture full details from original matchSELECTc.tx_id,c.symbol,c.outer_instruction_index,c.start_inner_index,c.end_inner_index,