当前位置:网站首页>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
边栏推荐
- C# 41. int与string互转
- POI excel conversion tool
- Analyzing the practical development of robot teaching
- An in-depth analysis of the election mechanism in kubernetes
- Shell脚本批量修改文件目录权限
- What are the design requirements for PCB layout and wiring?
- 浅谈软件研发的复杂性与效能提升之道
- Win 10创建一个gin框架的项目
- 618 activity season - the arrival of special discounts for hundreds of low code platforms
- tensorboard 使用总结
猜你喜欢

About Statistical Distributions

声网 VQA:将实时互动中未知的视频画质用户主观体验变可知

微软独家付费功能,也被完美解锁了

Win 10创建一个gin框架的项目

Mybayis之核心主件分析

Analyzing the practical development of robot teaching

What are the design requirements for PCB layout and wiring?

Advanced - Introduction to business transaction design and development

双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究

618 activity season - the arrival of special discounts for hundreds of low code platforms
随机推荐
PMP怎么补考?补考费用是多少?
Cvpr2022 | Zhejiang University and ant group put forward a hierarchical residual multi granularity classification network based on label relation tree to model hierarchical knowledge among multi granu
进阶高级-业务事务设计 开发入门
Modular operation
First day of new work
About Statistical Distributions
使用Karmada实现Helm应用的跨集群部署
Lumiprobe ProteOrange 蛋白质凝胶染料说明书
async-validator.js数据校验器
Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour
福建的朋友们,你们的养老保险上云啦!
PHP使用栈解决迷宫问题
19.2 容器分类、array、vector容器精解
FFmpeg学习总结
Pipeline based hybrid rendering
openGauss内核:SQL解析过程分析
数字化转型的1个目标,3大领域,6大因素和9个环节
【Unity3D】发射(RayCast)物理射线(Ray)
Business layer modification - reverse modification based on the existing framework
618活动季——百数低代码平台特享折扣来临