I have the following 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 |
What I need to do is, select all users where they have logged on to the same site for 5 hrs of first logging. My current approach is
logged_time
and add 5 hours to it.siteId
count for each usersiteId
count for each user equals 1 then grab those users else reject that userI think my approach is quite good. But I can't figure out how to do that using impala and SQL. Any help would be highly appreciated! Thanks.
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 )