当前位置:网站首页>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互转
- C语言文件操作
- 1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
- 180.1.连续登录N天(数据库)
- About Critical Values
- 1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
- Steam education to break the barriers between disciplines
- 做跨境电商一定要学会用PRA软件,解放双手提高效率!
- MindSpore系列一加载图像分类数据集
- async-validator.js数据校验器
猜你喜欢

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

Mybayis之核心主件分析

render函数解析

用户网络模型与QoE

解析机器人主持教学的实践发展

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

180.1. Log in continuously for n days (database)

CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识

About Covariance and Correlation(协方差和相关)

Chapter 2 processing files, cameras and GUI Cameo applications
随机推荐
内存泄露
原生实现.NET5.0+ 自定义日志
1 goal, 3 fields, 6 factors and 9 links of digital transformation
Mybayis之核心主件分析
An in-depth analysis of the election mechanism in kubernetes
解析机器人主持教学的实践发展
Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour
做跨境电商一定要学会用PRA软件,解放双手提高效率!
Memory leak
Can I open an account today and buy shares today? Is it safe to open an account online?
19.2 容器分类、array、vector容器精解
推荐两款超高质量的壁纸软件
ANR Application Not Responding
Shell script batch modify file directory permissions
Analyzing the practical development of robot teaching
匿名函数this指向以及变量提升
业务层修改--根据现有框架的反推修改
[unity3d] emission (raycast) physical ray (Ray)
About Statistical Distributions
Tensorboard Usage Summary