我有一个看起来像这样的表:
user_id user_action timestamp
1 action #2 2016-02-01 00:02
2 action #1 2017-10-05 15:24
3 action #3 2017-03-31 19:35
4 action #1 2017-07-09 00:24
1 action #1 2018-11-05 18:28
1 action #3 2018-02-01 13:02
2 action #2 2017-10-05 16:14
2 action #3 2017-10-05 16:34
etc
我的任务是编写一个查询,在那里我可以看到用户会话,其中用户按特定顺序执行操作#1,2和3,操作之间的时间间隔小于一小时。例如,用户#2有一个会话
2 action #1 2017-10-05 15:24
2 action #2 2017-10-05 16:14
2 action #3 2017-10-05 16:34
很抱歉没有我自己的尝试,因为我真的被卡住了,不知道,从哪里开始。 提前谢谢!
答案 0 :(得分:1)
这可以通过窗口函数lead
和lag
完成,它们可以分别获取下一行和上一行的值。
select distinct user_id
from (select user_id,user_action,timestamp,
lag(user_action) over(partition by user_id order by timestamp) as prev_action,
lead(user_action) over(partition by user_id order by timestamp) as next_action,
datediff(minute,lag(timestamp) over(partition by user_id order by timestamp),timestamp) as time_diff_with_prev_action,
datediff(minute,timestamp,lead(timestamp) over(partition by user_id order by timestamp)) as time_diff_with_next_action
from tbl
) t
where user_action='action#2' and prev_action='action#1' and next_action='action#3'
and time_diff_with_prev_action <= 60 and time_diff_with_next_action <= 60