楼主的语句本来就运行不了,上边那个瞎复制,都不运行一遍发什么发。oracle和sqlserver下运行语句如下:
select a.ticket_id,a.txn_station,b.txn_station from
(select row_number() over (partition by ticket_id order by txn_time) rn,subway.* from subway) a,
(select row_number() over (partition by ticket_id order by txn_time) rn,subway.* from subway) b
where a.ticket_id=b.ticket_id and a.trans_code=0 and b.trans_code=1 and a.txn_station<>b.txn_station and a.rn=b.rn-1
order by a.ticket_id
结果:
其他数据库另外说明
select first.ticker_id, first.txn_station as station1, s.station2, from Subway first inner join
(select second.ticker_id as t,second.txn_station as station2 from Subway second where second.trans_code='1') s on
first.ticker_id=s.t and first.trans_code='0';
试试