您可以使用窗口函數來檢索下一條記錄記錄中給出的當前記錄。
在函數上使用ORDER BY以確保事物符合時間戳順序並使用PARTITION保持時鐘分離,您可以爲每行找到它後面的行。
WITH links AS
(
SELECT
id, ts, clock, LEAD(ts) OVER (PARTITION BY clock ORDER BY ts) AS next_ts
FROM myTable
)
SELECT * FROM links
WHERE
EXTRACT(EPOCH FROM (next_ts - ts)) > 10
然後您可以比較時間戳。
窗口功能https://www.postgresql.org/docs/current/static/functions-window.html
或者,如果你喜歡使用派生表,而不是與條款。
SELECT * FROM (
SELECT
id, ts, clock, LEAD(ts) OVER (PARTITION BY clock ORDER BY ts) AS next_ts
FROM myTable
) links
WHERE
EXTRACT(EPOCH FROM (next_ts - ts)) > 10