当前位置:网站首页>sql面试题:求连续最大登录天数
sql面试题:求连续最大登录天数
2022-06-28 18:39:00 【南湖渔歌】
求用户最大连续登陆天数mysql实现
with t_1 as( -- 使用timestampdiff(unit, begin, end) <= 30进行where的条件筛选,并使用distinct函数进行清洗去重得到新表t_1
select DISTINCT dimMemberID as user_id, date(dimDateID) as login_date
from fct_sales
where timestampdiff(day, date(dimDateID),(select max(dimDateID) from fct_sales)) <= 30
and dimMemberID <> 0
order by login_date
),t_2 as( -- 得到一个按user_id分组并有排序编号的新表t_2
select user_id
,login_date
,row_number()over(partition by user_id order by login_date asc ) as ranks
from t_1
),t_3 as ( -- 使用date_sub函数获得新的列gap和新表t_3
select user_id
,login_date
,ranks
,date_sub(login_date, interval ranks day) as gap
from t_2
), t_4 as ( -- 对t_3表按user_id、gap进行分组,并对组内的login_date进行count计数,得到表t_4
select user_id
,gap
,count(login_date) as num
from t_3
group by user_id
,gap
) ,t_5 as ( -- 按照user_id进行分组,使用max取出最大的num
select user_id
,max(num) as series_login -- 按照user_id分组,求得最大连续登陆天数
from t_4
group by user_id
)
select * ,count(1)over() as cnt from t_5;
- with t_1 as
- timestampdiff
边栏推荐
- leetcode 1423. Maximum points you can obtain from cards
- Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
- Anonymous function this pointing and variable promotion
- 新工作第一天
- Cannot read property 'MTJ' of undefined
- Win 10创建一个gin框架的项目
- Alist+RaiDrive 给电脑整个80亿GB硬盘
- 1 goal, 3 fields, 6 factors and 9 links of digital transformation
- Analysis of the core components of mybayis
- 业务层修改--根据现有框架的反推修改
猜你喜欢

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour

1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import

Lumiprobe 蛋白质标记研究方案
![[unity3d] emission (raycast) physical ray (Ray)](/img/46/a9fda743f597db9584c982b10c191c.png)
[unity3d] emission (raycast) physical ray (Ray)

向上转型和向下转型

tensorboard 使用总结

抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱

curl: (56) Recv failure: Connection reset by peer

微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined
随机推荐
Win 10创建一个gin框架的项目
async-validator.js数据校验器
Opengauss kernel: analysis of SQL parsing process
leetcode 1689. Partitioning into minimum number of deci binary numbers
Opencv中使用Tracker实现物体跟踪
匿名函数变量问题
Steam education to break the barriers between disciplines
福建的朋友们,你们的养老保险上云啦!
618 activity season - the arrival of special discounts for hundreds of low code platforms
Business layer modification - reverse modification based on the existing framework
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
做跨境电商一定要学会用PRA软件,解放双手提高效率!
Modular operation
内存抖动
闭包的理解
基于管线的混合渲染
About Covariance and Correlation(协方差和相关)
Pipeline based hybrid rendering
抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱
Rxjs map, mergeMap 和 switchMap 的区别和联系