Query editor
WITH target_first_buys AS (-- First buy time per token for the target wallet (only meaningful positions)SELECTtoken_bought_mint_address AS token_mint,MIN(block_time) AS first_buy_time,SUM(amount_usd) AS total_bought_usdFROM dex_solana.tradesWHERE trader_id = '{{target_wallet}}'AND block_date >= CURRENT_DATE - INTERVAL '{{lookback_days}}' DAYAND amount_usd >= {{min_trade_usd}}GROUP BY token_bought_mint_addressHAVING SUM(amount_usd) >= {{min_position_usd}}),preceding_buys AS (-- For each token the target bought, find who bought it first within the windowSELECTt.trader_id AS preceding_wallet,t.token_bought_mint_address AS token_mint,MIN(t.block_time) AS their_first_buy,MIN(c.first_buy_time) AS target_first_buy,date_diff('second', MIN(t.block_time), MIN(c.first_buy_time)) AS seconds_beforeFROM dex_solana.trades tINNER JOIN target_first_buys cON t.token_bought_mint_address = c.token_mintWHERE t.block_date >= CURRENT_DATE - INTERVAL '{{lookback_days}}' DAYAND t.block_time < c.first_buy_timeAND t.block_time >= c.first_buy_time - ({{window_minutes}} * INTERVAL '1' MINUTE)AND t.trader_id != '{{target_wallet}}'AND t.amount_usd >= {{min_trade_usd}}GROUP BY t.trader_id, t.token_bought_mint_address)SELECTpreceding_wallet,COUNT(DISTINCT token_mint) AS tokens_preceded,COUNT(*) AS total_preceding_trades,ROUND(AVG(seconds_before) / 60.0, 2) AS avg_min_before,MIN(seconds_before) AS min_sec_before,MAX(seconds_before) AS max_sec_before,ROUND(SUM(CASE WHEN seconds_before <= 60 THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 1) AS pct_within_1min,ROUND(SUM(CASE WHEN seconds_before <= 300 THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 1) AS pct_within_5minFROM preceding_buysGROUP BY preceding_walletHAVING COUNT(DISTINCT token_mint) >= {{min_overlap_tokens}}ORDER BY tokens_preceded DESC, avg_min_before ASCLIMIT 25