我有一张桌子
event_id eventtypeid eventtimestamp
描述可以彼此接近的时间事件。我想找到事件对,其中eventtypes相等,时间戳之间的差异不是logner而不是3毫秒。
在sql中一般是否可以?我该如何制定这样的要求?
提前谢谢。
答案 0 :(得分:3)
mget
答案 1 :(得分:3)
这些查询通常最好用window function:
解决SELECT eventtypeid, first, second, diff
FROM (
SELECT eventtypeid, event_id AS first, lead(event_id) OVER w AS second,
lead(eventtimestamp) OVER w - eventtimestamp AS diff
FROM event_table
WINDOW w AS (PARTITION BY eventtypeid ORDER BY eventtimestamp)
) sub
WHERE diff <= interval '3 milliseconds';
这通常比自我加入快得多。