当前位置:网站首页>180.1. Log in continuously for n days (database)
180.1. Log in continuously for n days (database)
2022-06-28 18:54:00 【Drink more hot water today】
180.1. Continuous login N God ( database )
The original table

Use lag&lead+datediff Window function
An example of a miscalculation
| 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 |
![]() | ![]() |
Last time I was looking for consecutive numbers , You can find consecutive dates by changing them , Just use one more datediff Function is OK .
But what's different this time is , Each date has a corresponding user . Although the dates are continuous , If the date does not belong to the same user , Then it can't be counted .
chart 1 Because there is no grouping , direct order by, The result will be id by 2 Of users are also counted
SQL Code
Log in for three days , The output is 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
-- Have a hybrid
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
Log in for two consecutive days , The output is 1 and 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
Use date_sub function
I use it msql, Different databases have different syntax
SELECT DATE_ADD('2022-02-21', INTERVAL 12 DAY), DATE_SUB('2022-02-23', INTERVAL 13 DAY)
>> Output results
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;
边栏推荐
- 从知名软件提取出的神器,吊打一众付费
- 百度时间因子添加
- Business layer modification - reverse modification based on the existing framework
- 泰山OFFICE技术讲座:WORD奇怪的字体高度
- 3D rotatable particle matrix
- C# 41. int与string互转
- leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
- 牛津大学教授Michael Wooldridge:AI社区近40年如何看待神经网络
- 数字化转型的1个目标,3大领域,6大因素和9个环节
- Shanghai Pudong Development Bank Software Test interview real question
猜你喜欢
随机推荐
devpi
牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
向上转型和向下转型
About Covariance and Correlation(协方差和相关)
Qt 中 QObjectCleanupHandler 使用总结
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
Native implementation Net5.0+ custom log
原生实现.NET5.0+ 自定义日志
深入解析kubernetes中的选举机制
使用Karmada实现Helm应用的跨集群部署
被315点名的流氓下载器,又回来了…
Upload file list (repeated file names are marked with brackets)
Openharmony - detailed source code of Kernel Object Events
Record an emotet Trojan horse handling case
ANR Application Not Responding
MongoDB系列之MongoDB工作原理简单介绍
About Critical Values
百度时间因子添加
技术管理进阶——管理者如何做绩效沟通及把控风险
180.1.连续登录N天(数据库)














