简体   繁体   English

分组连续时间戳(红移)

[英]Grouping Consecutive Timestamps (Redshift)

Got something that I cant get my head around有一些我无法理解的东西

raw data shows every 15 min intervals and I would like to group them based on if they are consecutive 15 min intervals (see screenshot below) I will like to do this multiple times for each user and for alot of users... Any ideas on how to do this using sql only that can scale to 1000's users?原始数据每隔 15 分钟显示一次,我想根据它们是否是连续的 15 分钟间隔对它们进行分组(见下面的屏幕截图)我想为每个用户和很多用户多次执行此操作......关于如何仅使用可以扩展到 1000 个用户的 sql 来做到这一点?

在此处输入图像描述

Any help would be appreicated任何帮助将不胜感激

Thanks谢谢

This is a type of gaps-and-islands problem.这是一种差距和孤岛问题。 Use lag() to get the difference, then a cumulative sum to identify the group:使用lag()获取差异,然后使用累积和来识别组:

select user_id, min(start_time), max(end_time)
from (select t.*,
             sum( case when prev_end_time <> start_time then 0 else 1 end) over (partition by user_id order by start_time) as grp
      from (select t.*,
                   lag(end_time) over (partition by user_id order by start_time) as prev_end_time
            from t
           ) t
     ) t
group by user_id, grp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM