当前位置:网站首页>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

边栏推荐
- Database design specification
- 对象存储Minio使用教程
- RHEL7 切换字符编码为GBK
- Lvgl Chinese font production
- B_ QuRT_ User_ Guide(16)
- Shangpinhui mall_ Background homepage of
- Resolved: JDBC connection to MySQL failed with an error:'The last packet sent successfully to the server was 0 milliseconds ago. '
- 【ELT.ZIP】OpenHarmony啃论文俱乐部——电子设备软件更新压缩
- 摘桃子(双指针)
- [cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)
猜你喜欢
![[safety science popularization] have you been accepted by social workers today?](/img/ac/a6d2aa48219d02d82240d2f5343f19.jpg)
[safety science popularization] have you been accepted by social workers today?

Simple image browsing with fragment
![[safety science popularization] mining technology starts from the love story of a man of science and Engineering](/img/01/73376c133c33527e479685f8680238.jpg)
[safety science popularization] mining technology starts from the love story of a man of science and Engineering

【ELT.ZIP】OpenHarmony啃论文俱乐部——电子设备软件更新压缩

Understand single chip microcomputer drive 8080lcd

Instructor add function_ Enable auto fill_ Instructor modification function

B_ QuRT_ User_ Guide(16)

蓄力618 ,苏宁如何打下这场硬仗?

J. Balanced Tree

/10个值得推荐的学习编程的网站 世界已经进入了互联网的时代。据最近发布的一篇《2016年互联网趋势》报告显示,中国已成为互联网市场的领导者,中国互联网用户的数量达到了6.68亿。可以预见,有
随机推荐
SQL查询连续三天登录的用户
If no separation ----- > > login module nanny level source code analysis (0)
多线程交替输出AB
SSL交互过程
B_ QuRT_ User_ Guide(18)
The tide play power is really firepower! The first big screen cinema for young people? Cool open TV Max 86 "sudden attack
基于SSM框架的学生在线教育教学课程管理系统
被“内卷”酸翻的OPPO Reno6
OpenGL第十一章 多光源
js实现柯里化
Azure kubernates service update | improve development experience and efficiency
多线程四部曲之pthread
JS to realize coritization
RequestContextHolder
B_ QuRT_ User_ Guide(16)
PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
SSL库选择
[cloud native] what is micro service? How to build it? Teach you how to build the first micro service (framework)
J. Balanced Tree
Jeecgboot learning_ Online form first experience