当前位置:网站首页>SQL continuous login problem
SQL continuous login problem
2022-06-29 02:38:00 【Why not sell egg cakes well】
sql Continuous login problem
Today we have a video , Requirements are calculated in a library table , Inquire about 2022 year 1 Users who log in for three consecutive days in the month and the duration of continuous login .
First ,t_login It records the time information of each login .
Log in multiple times a day , Just record one ;
( Using functions invalidates the index )
select distinct uid,date(login_time) ymd
from t_login
where login_time between '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59';
Rough way , Self associate the above table three times , shortcoming , Expandability of .
select t1.uid,t1.ymd,t2.ymd,t3.ymd
from
(select distinct uid,date(login_time) ymd
from t_login
where login_time between '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59') t1
join
(select distinct uid,date(login_time) ymd
from t_login
where login_time between '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59') t2
on
(t1.uid=t2.uid and datediff(t2.ymd,t1.tmd)=1)
join
(select distinct uid,date(login_time) ymd
from t_login
where login_time between '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59') t3
on
(t2.uid=t3.uid and datediff(t3.ymd,t2.tmd)=1);
Mode two
Use window functions , Partition restrictions , obtain
with t1 as (select distinct uid,date(login_time) ymd
from t_login
where login_time between timestamp '2022-01-01 00:00:00'
and timestamp '2022-01-31 23:59:59')
select uid,ymd,
row_number() over (partition by uid order by ymd) num
from t1;

As long as it's not continuous , There must be a result jump in interpolation .
with t1 as (select distinct uid,date(login_time) ymd
from t_login
where login_time between timestamp '2022-01-01 00:00:00'
and timestamp '2022-01-31 23:59:59')
t2 as(select uid,ymd,
date_sub(ymd,interval row_number() over (partition by uid order by ymd) day) sub_date
from t1)
select uid,min(ymd),max(ymd),count(*)
from t2
group by uid,sub_date
having count(*)>=3;
Fucking regret , It's a waste of time . Alas, I have no time to sigh .
边栏推荐
- thinkphp5.1 runtime文件改成777权限了, 还是无法写入
- CTFHub-Web-SQL注入-整数型注入
- 项目研发,有哪些好用的免费脑图工具软件
- 组合数据类型之元组小练习
- LabVIEW jump to web page
- String segment combination
- 对补wasm环境的一些测试
- "The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
- 大智慧手机股票开户哪个券商更安全更方便?
- After today, I look forward to the new year's eve of the year of the rabbit
猜你喜欢

How does sound amplify weak sounds

Relations EMC, EMI, EMS

PWN攻防世界Level2

Leetcode counts the logarithm of points that cannot reach each other in an undirected graph

兰宝传感科技冲刺科创板:年营收3.5亿 许永童家族色彩浓厚

To apply for a test engineer after years, the resume with high scores should be written like this

Have you learned the common SQL interview questions on the short video platform?

学习太极创客 — MQTT 第二章(九)本章测试

PMP项目管理概述

“内窥镜第一股”二闯IPO,去年亏损5个亿,核心产品商业化仍存疑 | IPO速递
随机推荐
KOA Quick Start
PWN新手入门Level0
chrome浏览器关闭更新弹窗
Koa 快速入門
安装kibana
Summary of several days
PHP的system函数
PHP的exec函数
Eliminate the hover effect when the button is disabled
CTFHub-Web-SQL注入-整数型注入
HashSet storing objects and how to not store the same objects
项目研发,有哪些好用的免费脑图工具软件
三角函数计算
11 go Foundation: Interface
组合数据类型之元组小练习
Ctfhub web SQL injection - integer injection
mgalcu-a509
fsockopen函数的应用
大智慧手机股票开户哪个券商更安全更方便?
对补wasm环境的一些测试