https://www.youtube.com/watch?v=B7sTTafjXcg
create table events
(userid int ,
event_type varchar(20),
event_time datetime);
insert into events VALUES (1, 'click', '2023-09-10 09:00:00');
insert into events VALUES (1, 'click', '2023-09-10 10:00:00');
insert into events VALUES (1, 'scroll', '2023-09-10 10:20:00');
insert into events VALUES (1, 'click', '2023-09-10 10:50:00');
insert into events VALUES (1, 'scroll', '2023-09-10 11:40:00');
insert into events VALUES (1, 'click', '2023-09-10 12:40:00');
insert into events VALUES (1, 'scroll', '2023-09-10 12:50:00');
insert into events VALUES (2, 'click', '2023-09-10 09:00:00');
insert into events VALUES (2, 'scroll', '2023-09-10 09:20:00');
insert into events VALUES (2, 'click', '2023-09-10 10:30:00');
WITH
TIMESTAMPDIFF()
CASE WHEN
WITH cte AS (
select *
,lag(event_time,1,event_time) OVER (partition by userid order by event_time) as prev_time,
IFNULL(timestampdiff(minute,lag(event_time,1) OVER (partition by userid order by event_time),event_time),0) AS diff
from events
),cte2 as (
SELECT *,
SUM(CASE WHEN diff <= 30 THEN 0 ELSE 1 END) OVER (partition by userid order by event_time) AS Sesh_grp
FROM cte
)
SELECT userid,sesh_grp+1,min(event_time) AS Sesh_start,max(event_time) AS sesh_end,count(*) AS num_events,timestampdiff(minute,min(event_time),max(event_time)) AS sesh_duration
FROM cte2
group by userid,sesh_grp
order by userid,sesh_grp