当前位置:网站首页>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;
边栏推荐
- SMARCA2抗体研究:Abnova SMARCA2 单克隆抗体方案
- 进阶高级-业务事务设计 开发入门
- use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
- Collection of real test questions
- devpi
- 内存抖动
- OpenHarmony—内核对象事件之源码详解
- MindSpore系列一加载图像分类数据集
- 匿名函数变量问题
- idea其他分支合并到dev分支
猜你喜欢

618 activity season - the arrival of special discounts for hundreds of low code platforms

模块化操作

3D rotatable particle matrix

use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue

180.1.连续登录N天(数据库)

19.2 container classification, array and vector container refinement

19.2 容器分类、array、vector容器精解

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

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

电子商务盛行,怎么提高商店转换率?
随机推荐
匿名函数this指向以及变量提升
Lumiprobe ProteOrange 蛋白质凝胶染料说明书
Baidu time factor addition
毕业设计-基于Unity的餐厅经营游戏的设计与开发(附源码、开题报告、论文、答辩PPT、演示视频,带数据库)
手动备份和还原DHCP服务器
深入解析kubernetes中的选举机制
Advanced - Introduction to business transaction design and development
MindSpore系列一加载图像分类数据集
使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
19.2 container classification, array and vector container refinement
curl: (56) Recv failure: Connection reset by peer
Enhancing steam and engineering education from theory to practice
Anonymous function variable problem
Installing the nodejs environment
BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤
推荐两款超高质量的壁纸软件
ANR Application Not Responding
原生实现.NET5.0+ 自定义日志
OpenHarmony—内核对象事件之源码详解
SMARCA2抗体研究:Abnova SMARCA2 单克隆抗体方案




