当前位置:网站首页>SQL continuous login problem
SQL continuous login problem
2022-06-30 19:29:00 【Game programming】
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) ymdfrom t_loginwhere 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) ymdfrom t_loginwhere login_time between '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59') t1join(select distinct uid,date(login_time) ymdfrom t_loginwhere login_time between '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59') t2on(t1.uid=t2.uid and datediff(t2.ymd,t1.tmd)=1)join(select distinct uid,date(login_time) ymdfrom t_loginwhere login_time between '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59') t3on(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) numfrom 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_datefrom t1)select uid,min(ymd),max(ymd),count(*)from t2group by uid,sub_datehaving count(*)>=3; Fucking regret , It's a waste of time . Alas, I have no time to sigh .
author : Why not sell egg cakes well
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
猜你喜欢

ArcGIS no plug-in load (no offset) day map

Business Intelligence BI and business management decision-making thinking 4: business cost analysis

微信小程序快速入门 --项目介绍

How to seamlessly transition from traditional microservice framework to service grid ASM

Task01:初识数据库与SQL(笔记1)

Task01: getting to know database and SQL (Note 1)

Kalman滤波器--从高斯融合推导

Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched

连接实验室服务器

为什么越来越多的人选择云渲染?
随机推荐
浏览器窗口切换激活事件 visibilitychange
CODING 正式入驻腾讯会议应用市场!
基于STM32单片机的测温仪
MySQL function to get the full path
BeanUtils.copyProperties() 对比 mapstruct
「经验」爬虫在工作中的实战应用『实现篇』
Go redis connection pool
Cloud Native Landing Practice Using rainbond for extension dimension information
com. alibaba. fastjson. Jsonobject tojsonstring eliminate circular reference
Connect to lab server
torch stack() meshgrid()
mysql函数获取全路径
Entropy - conditional entropy - joint entropy - mutual information - cross entropy
slice
Practical application of "experience" crawler in work
Kalman滤波器--从高斯融合推导
Task04:集合运算-表的加减法和join等--天池龙珠计划SQL训练营学习笔记
一文详解|Go 分布式链路追踪实现原理
如何使用物联网低代码平台进行服务管理?
Gateway服务网关