我使用以下查询来获取两个时间戳之间的差异:
SELECT tracker_id,
TIMESTAMP,
LAG(TIMESTAMP) OVER(ORDER BY TIMESTAMP DESC),
TIMESTAMPDIFF(MINUTE,
TIMESTAMP,
LAG(TIMESTAMP) OVER(ORDER BY TIMESTAMP DESC)) AS diff_in_minutes
FROM comm_telemetry
WHERE comm_telemetry.tracker_id = "123456789"
ORDER BY comm_telemetry.timestamp DESC;
我想过滤结果,只在diff_in_minutes〉0时显示。问题是,WHERE子句中不允许使用窗口函数。
有什么建议怎么这么解决这个?
2条答案
按热度按时间hfsqlsce1#
您将需要首先计算子查询中的滞后时间,然后再次查询该滞后时间以使用它进行筛选。
hjzp0vay2#
同时找到了一个解决方案: