当前位置:网站首页>SQL query users logged in for three consecutive days
SQL query users logged in for three consecutive days
2022-06-11 03:36:00 【Xinxin s】
Ideas :
1、 Because users may log in more than once a day , Therefore, you need to reset the user's login date every day .
2、 Reuse row_number() over(partition by _ order by _) The function will the user id grouping , Sort by login date .
3、 Calculate the login date minus the result value obtained in step 2 , When the user logs in continuously , The result of subtraction is the same .
4、 according to id And date group and count , Filter is greater than or equal to 3 Is continuous 3 Days of users .
The database I use is SQL Server 2014
Create table statement :
create table user_login(
user_id int,
login_time datetime,
)
Add data :
insert into user_login values (1,'2022-06-01 11:00:00.000');
insert into user_login values (1,'2022-06-01 12:00:00.000');
insert into user_login values (1,'2022-06-01 12:00:00.000');
insert into user_login values (1,'2022-06-02 11:00:00.000');
insert into user_login values (1,'2022-06-03 11:00:00.000');
insert into user_login values (2,'2022-06-01 11:00:00.000');
insert into user_login values (2,'2022-06-02 11:00:00.000');
insert into user_login values (2,'2022-06-04 11:00:00.000');
insert into user_login values (3,'2022-06-01 11:00:00.000');
insert into user_login values (3,'2022-06-02 11:00:00.000');
insert into user_login values (3,'2022-06-04 11:00:00.000');
insert into user_login values (3,'2022-06-05 11:00:00.000');
insert into user_login values (3,'2022-06-06 11:00:00.000');
insert into user_login values (3,'2022-06-07 11:00:00.000');
insert into user_login values (3,'2022-06-08 11:00:00.000');
Query statement
select
B.user_id
from
(
select
A.user_id,
A.login_date,
datediff (day,A.login_date,A.rn) AS inteval_days
from
(
select
user_id,
CONVERT(varchar(100), login_time, 23) login_date,
row_number() over (partition by user_id order by CONVERT(varchar(100), login_time, 23)) as rn
from
user_login
)A
)B
group by B.user_id,B.inteval_days
having count(1) >= 3;
Query results

边栏推荐
- 單片機通信數據延遲問題排查
- Oppo reno6 turned sour by "inner roll"
- 被“内卷”酸翻的OPPO Reno6
- Canvas+svg line particle animation web page background
- Dépannage du problème de retard des données de communication du micro - ordinateur à puce unique
- 路径计数2(dp + 组合数)
- canvas绘图——如何把图形放置画布中心
- Arm development board scheme and manufacturer analysis
- LVGL中文字体制作
- /The world of 10 recommended websites for learning programming has entered the era of the Internet. According to a recently released Internet trends 2016 report, China has become a leader in the Inter
猜你喜欢

If the source code of the home page module is not separated ----- > nanny level source code analysis (1)

正则表达式

618 coming! Can oppo reno6, which is sold through all channels with high price and low configuration, win?

/The world of 10 recommended websites for learning programming has entered the era of the Internet. According to a recently released Internet trends 2016 report, China has become a leader in the Inter

thinkphp3.2.3反序列化利用链分析

{dataSource-1} closing ... {dataSource-1} closed

canvas交互式星星动画背景js特效

UML系列文章(28)体系结构建模---协作

Oppo reno6 turned sour by "inner roll"

Canvas+svg line particle animation web page background
随机推荐
UML系列文章(28)体系结构建模---协作
Why is vfly, a high-end brand of Yadi that does not live up to its name, not high-end?
正则表达式
SSL库选择
Solution to the problem of gd32f4 serial port DMA reception
【ELT.ZIP】OpenHarmony啃论文俱乐部——多层存储分级数据压缩
PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
基于SSM框架的连锁超市购物零售后台管理系统
RHEL7 切换字符编码为GBK
OpenGL Chapter 7 basic lighting
單片機通信數據延遲問題排查
[safety science popularization] have you been accepted by social workers today?
jlink最新版本下载
J. Balanced Tree
C. Jump and Treasure(dp + 单调队列优化)
OpenGL Chapter 10 illuminant
Delete the watermark of the picture uploaded by CSDN
Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
基于SSM的考试项目管理系统
PostgreSQL source code learning (21) -- fault recovery ② - transaction log initialization