当前位置:网站首页>sql连续登录问题
sql连续登录问题
2022-06-30 18:16:00 【游戏编程】
sql连续登录问题
今天刷到一个视频,需求是计算一个库表里,查询2022年1月份里连续三天登录的用户以及连续登录的时间段。
首先,t_login记录了用户每次登录的时间信息。
一天有多次登录行为,只需记录一条;
(使用函数导致索引失效)
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';比较粗暴方式,将上面的表自关联三次,缺点,扩展性差。
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);方式二
使用窗口函数,分区限制条件,得到
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; 
只要不连续,插值一定存在结果跳跃。
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; 妈的后悔了,浪费时间了。哎没时间感叹。
作者:为什么不好好卖蛋饼
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- Swin-transformer --relative positional Bias
- Kalman filter -- Derivation from Gaussian fusion
- Pyth Solana is a bridge connecting reality on the chain
- 「干货」数据分析常用的10种统计学方法,附上重点应用场景
- 基于STM32F1的环境光与微距离检测系统
- 基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务
- Temperature measuring instrument based on STM32 single chip microcomputer
- 基于UDP协议设计的大文件传输软件
- 教你Selenium 测试用例编写
- torch. roll
猜你喜欢

Detailed single case mode

华兴证券:混合云原生架构下的 Kitex 实践

链表中环的入口结点-链表专题

20220528【聊聊假芯片】贪便宜往往吃大亏,盘点下那些假的内存卡和固态硬盘

Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 1)

浏览器窗口切换激活事件 visibilitychange

How to use the low code platform of the Internet of things for service management?

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)

Cloud Native Landing Practice Using rainbond for extension dimension information

Opencv data type code table dtype
随机推荐
MySQL modify data type_ MySQL modify field type [easy to understand]
20220607跌破建议零售价,GPU市场正全面走向供过于求...
Teach you how to write selenium test cases
MySQL recursion
NBI可视化平台快速入门教程(五)编辑器功能操作介绍
教你Selenium 测试用例编写
Nodejs 安装与介绍
年复一年,为什么打破数据孤岛还是企业发展的首要任务
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 1)
Practical application of "experience" crawler in work "theory"
浏览器窗口切换激活事件 visibilitychange
Large file transfer software based on UDP protocol
如何使用物联网低代码平台进行服务管理?
TCP packet sticking problem
嵌入式软件开发新趋势:DevOps
Unity technical manual - preliminary performance optimization
com.alibaba.fastjson.JSONObject # toJSONString 消除循环引用
rust配置国内源
Memory Limit Exceeded
How to improve the three passive situations in data analysis