在 oracle sql 中配对记录

To pair up records in oracle sql

这是我的sql: 执行时,它说

  1. 00000 - "missing keyword" on the position of "CROSS APPLY".

我只是想将一些记录(在一天内 =20160720)与 table 中的相同 TICKET_ID 和 return 他们的 T_TIME 和 T_LOCATION.

select a.T_TIME, b.T_TIME, a.T_LOCATION, b.T_LOCATION 
FROM TABLE a 
CROSS APPLY 
(select * from TABLE b where a.TICKET_ID = b.TICKET_ID having count(TICKET_ID) > 1) b
where (a.T_DATE=20160720);

是使用CROSS APPLY引起的问题吗?

好了,原来我想解决的问题是:)

table 看起来像这样:

T_TIME        |T_LOCATION   |    TICKET_ID|T_DATE
20160720091032|      ---0103|       1A268F|20160720
20160720095842|      ---0115|       63T37H|20160720
20160720133408|      ---0124|       1A268F|20160720
20160721152400|      ---0116|       598I3R|20160721
20160720125844|      ---0147|       63T37H|20160720

我想将具有相同 TICKET_ID 的记录配对。 2 条记录共享一条记录 TICKET_ID。我想要这样的输出:

20160720091032|20160720133408|0103|0124|
20160720095842|20160720125844|0115|0147|

table 非常大,比如 T_DATE=20160720 总共会有 200000 条记录。

一种方法是:

select a.ticket_id, a.t_time, b.t_time, a.t_location, b.t_location
from the_table a
  join the_table b on a.ticket_id = b.ticket_id and a.t_time < b.t_time
where a.t_date = 20160720;

连接条件 and a.t_time < b.t_time 确保一对的 "other" 版本不在结果中,例如你只会得到 (0103, 0124) 但不会得到 (0124, 0103)。