Query editor
-- StormTrade TON: Daily PnL / Volume / Fees / Liquidations (session-based, using your original logic + corrected liquidation PnL)WITH-- 1) Raw events from update_position and complete_orderevents_raw AS (SELECTtrace_id,user_position,direction,tx_lt,tx_now,position_size,position_open_notional,position_margin,amm,oracle_price,position_fee,origin_op,CASEWHEN origin_op = 2774268195 THEN 'increase'ELSE 'decrease_or_close'END AS event_type,2 AS src_rankFROM stormtrade_ton.update_positionUNION ALLSELECTtrace_id,user_position,direction,tx_lt,tx_now,position_size,position_open_notional,position_margin,amm,oracle_price,position_fee,origin_op,CASEWHEN origin_op = 2774268195 THEN 'increase'ELSE 'decrease_or_close'END AS event_type,1 AS src_rankFROM stormtrade_ton.complete_order),-- 2) Deduplicate per (trace_id, amm), prefer complete_orderevents AS (SELECT *FROM (SELECTer.*,ROW_NUMBER() OVER (PARTITION BY er.trace_id, er.ammORDER BY er.src_rank, er.tx_lt, er.trace_id