Query editor
WITH storm_data_sources AS (SELECT * FROM stormtrade_ton.update_position),storm_order_completion AS (SELECT * FROM stormtrade_ton.complete_order),storm_order_execution AS (SELECT * FROM stormtrade_ton.execute_order),storm_trading_alerts AS (SELECT * FROM stormtrade_ton.trade_notification),position_tracking_raw AS (SELECT user_position, sds.direction, position_size, tx_lt AS position_timeFROM storm_data_sources sdsUNION ALLSELECT user_position, soc.direction, position_size, tx_lt AS position_timeFROM storm_order_completion soc),position_tracking_clean AS (SELECT DISTINCT * FROM position_tracking_raw),position_history AS (SELECTuser_position,direction,position_time,COALESCE(LAG(position_size, 1) OVER(PARTITION BY user_position, directionORDER BY position_time ASC),CAST(0 AS DECIMAL)) AS previous_sizeFROM position_tracking_clean),trader_asset_mapping AS (SELECT DISTINCTsta.trader_addr,sds.user_position,sta.asset_idFROM storm_data_sources sdsJOIN ton.messages msg_outboundON msg_outbound.block_date = sds.block_dateAND msg_outbound.tx_hash = sds.tx_hashAND msg_outbound.direction = 'out'JOIN ton.messages msg_inboundON msg_outbound.block_date = msg_inbound.block_dateAND msg_outbound.msg_hash = msg_inbound.msg_hashAND msg_inbound.direction = 'in'JOIN storm_trading_alerts staON sta.tx_hash = msg_outbound.tx_hashWHERE sds.origin_op != 3427973859UNION ALLSELECT DISTINCT