欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

SQL面试题-统计连续n天登陆的用户(开窗函数,date

时间:2023-06-23

统计连续 5 天登录的用户,login 登录表:

hive> select * from temp;OK2020-09-01a2020-09-01b2020-09-02a2020-09-03a2020-09-03b2020-09-04a2020-09-04b2020-09-05a2020-09-06a2020-09-07b2020-09-08a2020-09-10a2020-09-11a2020-09-11b2020-09-23b2020-09-24b2020-09-26b2020-09-27b2020-09-29b2020-09-30b2020-09-12b2020-09-17bTime taken: 0.085 seconds, Fetched: 22 row(s)hive> select distinct a.userid from (select dt,userid,row_number() over(partition by userid order by dt) as rank from temp) a join (select dt,userid,row_number() over(partition by userid order by dt) as rank from temp) b on a.userid = b.userid and a.rank = b.rank -5 and a.dt = date_add(b.dt,-5);OKaTime taken: 10.525 seconds, Fetched: 1 row(s)

SQL

select distinct a.userid from (select dt,userid,row_number() over(partition by userid order by dt) as rank from temp) a join (select dt,userid,row_number() over(partition by userid order by dt) as rank from temp) b on a.userid = b.userid and a.rank = b.rank -5 and a.dt = date_add(b.dt,-5);

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。