SQL如何检查彼此之间是否存在一小时范围内的时间戳
我有一个如下表,其中用户事件记录为unixtimeSQL如何检查彼此之间是否存在一小时范围内的时间戳,sql,timestamp,calculation,Sql,Timestamp,Calculation,我有一个如下表,其中用户事件记录为unixtime select day, user_id, unixtime, from_unixtime(unixtime) from sampletable 白天 用户id unixtime 时间戳 2020-12-20 1. 1608434879 2020-12-20 03:27:59.000 2020-12-20 1. 1608468153 2020-12-20 12:42:33.000 2020-12-20 2. 1608436224 2020-
select day,
user_id,
unixtime,
from_unixtime(unixtime)
from sampletable
白天
用户id
unixtime
时间戳
2020-12-20
1.
1608434879
2020-12-20 03:27:59.000
2020-12-20
1.
1608468153
2020-12-20 12:42:33.000
2020-12-20
2.
1608436224
2020-12-20 03:50:24.000
2020-12-20
2.
1608437616
2020-12-20 04:13:36.000
2020-12-20
3.
1608476189
2020-12-20 14:56:29.000
2020-12-20
3.
1608505424
2020-12-20 23:03:44.000
2020-12-20
3.
1608505438
2020-12-20 23:03:58.000
2020-12-20
4.
1608463622
2020-12-20 11:27:02.000
您可以使用铅:
对于比率,您可以除以countdistinct user_id。用您正在使用的数据库标记您的问题。
select day,
count(distinct case when next_unixtime - unixtime < 60*60 then user_id end) as num_users
from (select t.*,
lead(unixtime) over (partition by user_id, day order by unixtime) as next_unixtime
from sampletable
) t
group by day;