点击上方【蓝色】字体 关注我们

01 场景描述
02 数据准备
create table user_login as(select stack(8,'a', '2024-10-01 09:55','b', '2024-10-01 09:57','c', '2024-10-01 10:01','d', '2024-10-01 23:55','e', '2024-10-01 23:58','f', '2024-10-02 00:03','g', '2024-10-02 08:32','h', '2024-10-02 08:33') as (user_name, times))

03 问题分析
select user_name, times, lag(times, 1, 0) over (order by times) lag_timesfrom user_login

select user_name, times, NVL((unix_timestamp(times, 'yyyy-MM-dd hh:mm') - unix_timestamp(lag_times, 'yyyy-MM-dd hh:mm')) 60, 0) min_difffrom (select user_name, times, lag(times, 1, 0) over (order by times) lag_timesfrom user_login) t
select user_name, times, min_diff, sum(case when min_diff < 10 then 0 else 1 end ) over(order by times) flgfrom (select user_name, times, NVL((unix_timestamp(times, 'yyyy-MM-dd hh:mm') - unix_timestamp(lag_times, 'yyyy-MM-dd hh:mm')) 60,0) min_difffrom (select user_name, times, lag(times, 1, 0) over (order by times) lag_timesfrom user_login) t) t

步骤4: 将同一标记分组中的数据进行合并
select grp + 1 grp,concat_ws(',',collect_list(user_name)) user_namefrom(select user_name, times, min_diff, sum(case when min_diff < 10 then 0 else 1 end) over (order by times) grpfrom (select user_name, times, NVL((unix_timestamp(times, 'yyyy-MM-dd hh:mm') - unix_timestamp(lag_times, 'yyyy-MM-dd hh:mm')) 60,0) min_difffrom (select user_name, times, lag(times, 1, 0) over (order by times) lag_timesfrom user_login) t) t) tgroup by grp

04 小 结

会飞的一十六
微信号:ddan_hashcode
扫描右侧二维码关注我们
点个【在看】 你最好看

文章转载自会飞的一十六,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。






