如何 select 在整个 5 小时的时间段内具有相同 siteId 的 userIds?

How to select userIds that have same siteId throughout the 5 hrs of time period?

我有以下 table,

userId siteId logged_time
100001 7890 31-01-2021 11:55
100002 7878 31-01-2021 12:27
100001 7890 01-02-2021 01:05
100004 7878 01-02-2021 11:55
100002 7848 01-02-2021 11:30
100001 7890 02-02-2021 13:34

我需要做的是,select 首次登录同一站点 5 小时的所有用户。我目前的做法是

我觉得我的方法还是不错的。但我不知道如何使用 impala 和 SQL 来做到这一点。 任何帮助将不胜感激!谢谢。

SELECT *
FROM src t1
WHERE NOT EXISTS ( SELECT NULL
                   FROM src t2
                   WHERE t1.userId = t2.userId
                     AND t1.siteId != t2.siteId
                     AND t2.logged_time BETWEEN logged_time 
                                            AND logged_time + INTERVAL 5 HOUR )

我会为此推荐 window 个函数:

select userid
from (select t.*,
             min(logged_time) over (partition by userid) as min_logged_time
      from t
     ) t
where logged_time < min_logged_time + interval '5 hour'
group by userid
having min(siteid) = max(siteid);

我会注意到,这实际上通过返回没有重复的用户来回答您的问题。我还希望 window 函数比其他方法更快。