假设我们有一个包含两列的表:station_id和timestamp。我安装了两个气象站,它们设置为大约每12小时播放一次天气变化。这些流流了几分钟。
让我们将此表作为示例:
station_id timestamp
----------------------------------
station_1 2019-06-02 09:01:10
station_1 2019-06-02 09:01:13
station_1 2019-06-02 21:05:10
station_1 2019-06-02 21:08:15
station_2 2019-06-02 09:30:10
station_2 2019-06-02 09:31:10
station_2 2019-06-02 21:40:11
station_2 2019-06-02 21:40:12
我的目标是分析流式数据,并检查我的两个拖缆是否按常规定期流播。通常,我指的是每12个小时一次。
输出应如下所示
station_id timestamp
----------------------------------
station_1 2019-06-02 09:01:10
station_1 2019-06-02 21:05:10
station_2 2019-06-02 09:30:10
station_2 2019-06-02 21:40:11
我只考虑了一天,但它应该能够处理几天的数据。
我想我需要通过station_id和一个间隔进行分组,并仅显示间隔开始时间戳
最佳答案
我想您只想要lag()
。像这样:
select t.*
from (select t.*,
lag(timestamp) over (partition by station order by timestamp) as prev_timestamp
from t
) t
where prev_timestamp is null or
prev_timestamp < timestamp - interval 11 hour;
这仅需要11个小时而不是12个小时即可为重复测量提供一些余地。
请注意,日期/时间功能因数据库而异。以上适用于MySQL。在另一个数据库中可能略有不同。