当前位置:网站首页>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;
边栏推荐
猜你喜欢
随机推荐
今天开户今天能买股票吗?在线开户是安全么?
curl: (56) Recv failure: Connection reset by peer
CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识
Chapter 2 processing files, cameras and GUI Cameo applications
3D rotatable particle matrix
Huawei cloud AOM released version 2.0, and three features appeared
正版ST-link/V2 J-LINK JTAG/SWD引脚定义和注意事项
ANR Application Not Responding
tensorboard 使用总结
leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
从知名软件提取出的神器,吊打一众付费
Collection of real test questions
使用Karmada实现Helm应用的跨集群部署
解析机器人主持教学的实践发展
Lumiprobe非荧光叠丨氮化物研究丨3-叠丨氮丙醇
内存抖动
POI Excel转换工具
双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
180.1.连续登录N天(数据库)














