当前位置:网站首页>180.1.连续登录N天(数据库)
180.1.连续登录N天(数据库)
2022-06-28 18:30:00 【今天多喝热水】
180.1.连续登录N天(数据库)
原表

使用lag&lead+datediff窗口函数
一个计算有误的例子
| SELECT a.user_id, a.login_date AS day1, LEAD(a.login_date,1)OVER(ORDER BY a.user_id) AS day2, LEAD(a.login_date,2)OVER(ORDER BY a.user_id) AS day3 FROM login_log a | SELECT a.user_id, a.login_date AS day1, LEAD(a.login_date,1)OVER(PARTITION BY a.user_id ORDER BY a.user_id) AS day2, LEAD(a.login_date,2)OVER(PARTITION BY a.user_id ORDER BY a.user_id) AS day3 FROM login_log a |
![]() | ![]() |
上次是求连续出现的数字,变换一下就能求连续的日期,只需要多用一个datediff函数就行。
但这次不一样的是,每一个日期都有与之对应的用户。虽然日期是连续的,倘若日期不属于同一个用户,那么就不能计算在内。
图1就是因为没分组,直接order by,导致输出结果将id为2的用户也计算在内
SQL代码
连续登录三天,输出结果是1
SELECT DISTINCT t.user_id FROM
(
SELECT a.user_id,
a.login_date AS day1,
LEAD(a.login_date,1)OVER(PARTITION BY a.user_id ORDER BY a.user_id) AS day2,
LEAD(a.login_date,2)OVER(PARTITION BY a.user_id ORDER BY a.user_id) AS day3
FROM login_log a
) t
WHERE DATEDIFF(t.day2,t.day1)=1 AND DATEDIFF(t.day3,t.day2)=1
-- 来个混合型的
SELECT DISTINCT user_id
FROM
(SELECT user_id,
LAG(login_date,1) OVER(PARTITION BY user_id ORDER BY login_date) AS lag_login_date,
login_date,
LEAD(login_date,1) OVER(PARTITION BY user_id ORDER BY login_date) AS lead_login_date
FROM dwd.login_log)t1
WHERE DATEDIFF(login_date,lag_login_date)=1 AND DATEDIFF(lead_login_date,login_date)=1
SELECT user_id,
LAG(login_date,1) OVER(PARTITION BY user_id ORDER BY login_date) AS lag_login_date,
login_date,
LEAD(login_date,1) OVER(PARTITION BY user_id ORDER BY login_date) AS lead_login_date
FROM login_log
连续登录两天,输出结果是1和3
SELECT DISTINCT t.user_id FROM
(
SELECT a.user_id,
a.login_date AS day1,
LEAD(a.login_date,1)OVER(PARTITION BY a.user_id ORDER BY a.user_id) AS day2,
LEAD(a.login_date,2)OVER(PARTITION BY a.user_id ORDER BY a.user_id) AS day3
FROM login_log a
) t
WHERE DATEDIFF(t.day2,t.day1)=1
使用date_sub函数
我用的是msql,不同数据库之间语法会有差异
SELECT DATE_ADD('2022-02-21', INTERVAL 12 DAY), DATE_SUB('2022-02-23', INTERVAL 13 DAY)
>>输出结果
2022-03-05 2022-02-10
| SELECT user_id, login_date, RANK()OVER(PARTITION BY user_id ORDER BY login_date) rk FROM login_log | SELECT user_id,login_date,rk-1, DATE_SUB(login_date, INTERVAL t1.rk-1 DAY) AS con_login_date FROM (SELECT user_id,login_date, RANK()OVER(PARTITION BY user_id ORDER BY login_date) rk FROM login_log) t1 |
![]() | ![]() |
SELECT user_id,con_login_date,COUNT(*) nums
FROM
(
SELECT user_id,login_date,rk-1, DATE_SUB(login_date, INTERVAL t1.rk-1 DAY) AS con_login_date
FROM
(SELECT user_id,login_date,RANK()OVER(PARTITION BY user_id ORDER BY login_date) rk
FROM login_log) t1
)t2
GROUP BY user_id,con_login_date
HAVING COUNT(*) >= 3;
边栏推荐
- ONEFLOW source code parsing: automatic inference of operator signature
- 东方财富软件股票开户是靠谱的吗?在哪开户安全
- 基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
- 解析机器人主持教学的实践发展
- Upload file list (repeated file names are marked with brackets)
- How to design a business high performance and high availability computing architecture - job
- How to upgrade from RHEL 8 to RHEL 9
- postgresql数据库docker
- Lumiprobe丨Lumizol RNA 提取试剂解决方案
- 手动备份和还原DHCP服务器
猜你喜欢

Small program graduation project based on wechat campus lost and found graduation project opening report function reference

IDM certification process log embedding point description

Lumiprobe非荧光叠丨氮化物研究丨3-叠丨氮丙醇

Redis6 notes 04 master-slave replication, cluster, application problems, new redis6 features

内存泄露

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

Chapter 2 processing files, cameras and GUI Cameo applications

Does face recognition test involve privacy and security issues? A foreign company was urgently stopped

tensorboard 使用总结

What are the design requirements for PCB layout and wiring?
随机推荐
Go descending sort takes top n
Small program graduation project based on wechat mobile mall small program graduation project opening report function reference
Sword finger offer 11 Minimum number of rotation array
An error is reported when ActiveMQ is started. The 1883 port occupation problem is solved
注意!PMP紧急缓考今天就截止了!
How to manage interface documents efficiently and gracefully
HackTheBox-baby CachedView
19.2 容器分类、array、vector容器精解
【软件测试】2022年普通高等学校招生全国统一考试
CORBA 架构体系指南(通用对象请求代理体系架构)
EasyExcel 学习笔记
Introduction to apifox
PHP使用栈解决迷宫问题
电子商务盛行,怎么提高商店转换率?
324. swing sequencing II
Win 10创建一个gin框架的项目
ANR Application Not Responding
Apifox 介绍
GCC getting started manual
Easyexcel learning notes




