SQL面试题001--图文并茂解答连续登录问题

2024-06-01 20:52

本文主要是介绍SQL面试题001--图文并茂解答连续登录问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

连续登录问题是经典问题,今天做下总结。首先对原数据进行处理成客户和日期是不重复的,且日期是 yyyy-MM-dd 格式,这样好使用日期相关的函数。

本文参考在文末,增加了图表,更加容易理解。

表:temp01_cust_logon。

表字段和数据如下图的 A 和 B 列。

方法1: 利用窗口函数。

我们先对每个客户的登录日期做排序( 临时表:temp02_cust_logon2),然后对日期与排序的值进行相减得到 date_line( 临时表:temp03_cust_logon3)。因为如果是连续登录日期,那么减去连续的排序值就是相同的日期,再对相同的日期进行统计,超过3就是连续登录三天。

-- 利用窗口函数with temp02_cust_logon2 as
(selectt1.kehu_id,t1.date,row_number () over (partition by t1.kehu_id order by t1.date) as rnfromtemp01_cust_logon as t1
)
,temp03_cust_logon3 as
(selectt1.kehu_id,t1.date,t1.rn,date_sub(t1.date,t1.rn) as date_linefromtemp02_cust_logon2 as t1
)
-- select * from temp03_cust_logon3selectt1.kehu_id,t1.date_line,count(1) as cnt
fromtemp03_cust_logon3 as t1
group byt1.kehu_id,t1.date_line
havingcount(1) >= 3

image-20240601181402947

方法2:使用 lag (lead) 函数

首先看看这个函数如何使用。我本身数据是从20240510-20240525分区取的,所以使用这两个时间点来向前向后填充。

selectt1.kehu_id,t1.date,row_number () over (partition by t1.kehu_id order by t1.date) as rn ,lead(t1.date,2) over (partition by t1.kehu_id order by t1.date asc) as lead_date1 ,coalesce(lead(t1.date,2) over (partition by t1.kehu_id order by t1.date asc),'2024-05-25') as lead_date2 ,coalesce(lead(t1.date,3) over (partition by t1.kehu_id order by t1.date asc),'2024-05-25') as lead_date3,lag(t1.date,2) over (partition by t1.kehu_id order by t1.date asc) as lag_date1,coalesce(lag(t1.date,2) over (partition by t1.kehu_id order by t1.date asc),'2024-05-10') as lag_date2,coalesce(lag(t1.date,3) over (partition by t1.kehu_id order by t1.date asc),'2024-05-10') as lag_date3
fromtemp01_cust_logon as t1

lead 函数是想后面的数据向前位移,最后的位移的位置出现 NULL,可以用 coalesce 填充。我用相同的颜色表示位移的数据,这样就很好理解了。同样,lag 函数是将最前面的数据空出来,出现 NULL。还有一种写法,将出现NULL的位置填充自己想写的内容,不需要 coalesce 。

image-20240601181438761

但是实际上我想用客户本身最早和最近登录时间来填充,就得先建立临时表。注意标记红色的数据,和上面的数据做对比。

with temp01_cust_logon_minmax as 
(selectt1.kehu_id,max(t1.date) as max_date,min(t1.date) as min_datefrom temp01_cust_logon as t1group byt1.kehu_id
)
selectt1.kehu_id,t1.date,row_number () over (partition by t1.kehu_id order by t1.date) as rn ,lead(t1.date,2) over (partition by t1.kehu_id order by t1.date asc) as lead_date1 ,coalesce(lead(t1.date,2) over (partition by t1.kehu_id order by t1.date asc),t2.max_date) as lead_date2 ,coalesce(lead(t1.date,3) over (partition by t1.kehu_id order by t1.date asc),t2.max_date) as lead_date3,lag(t1.date,2) over (partition by t1.kehu_id order by t1.date asc) as lag_date1,coalesce(lag(t1.date,2) over (partition by t1.kehu_id order by t1.date asc),t2.min_date) as lag_date2,coalesce(lag(t1.date,3) over (partition by t1.kehu_id order by t1.date asc),t2.min_date) as lag_date3
fromtemp01_cust_logon as t1
left join temp01_cust_logon_minmax as t2
on t1.kehu_id = t2.kehu_id

image-20240601181549387

这是完整代码:我们对客户日期排序后,使用 lag 函数,这样就可以使用时间差函数计算。如果是连续登录,那么时间差是一样的。我们找的是连续登录三天,则找到出现 2 的时间差。然后再对时间差打标签,最后进行统计。

但是这里我们可以发现,20240513 这个最早登录日期被我人为填充后,时间差出现了异常,所以还是保留 NULL。我写了 date_diff2 ,date_line3 是我想要的标签字段,根据这个字段进行统计去重客户数。

with temp01_cust_logon_minmax as 
(selectkehu_id,max(date) as max_date,min(date) as min_datefrom temp01_cust_logongroup bykehu_id
)
,temp02_cust_logon2 as
(selectt1.kehu_id,t1.date,row_number () over (partition by t1.kehu_id order by t1.date) as rn ,lag(t1.date,2,t2.min_date) over (partition by t1.kehu_id order by t1.date asc) as lag_date,lag(t1.date,2,'0000-00-00') over (partition by t1.kehu_id order by t1.date asc) as lag_date2fromtemp01_cust_logon as t1left join temp01_cust_logon_minmax as t2on t1.kehu_id = t2.kehu_id
)
-- select * from temp02_cust_logon2,temp03_cust_logon3 as 
(selectt2.kehu_id,t2.date,t2.rn,t2.lag_date,date_diff(t2.date,t2.lag_date) as date_fiff,case when date_diff(t2.date,t2.lag_date) = 2 then 1 else 0 end as date_line1,if (date_diff(t2.date,t2.lag_date) = 2,1,0) as date_line2,date_diff(t2.date,t2.lag_date2) as date_fiff2,if (date_diff(t2.date,t2.lag_date2) = 2,1,0) as date_line3fromtemp02_cust_logon2 as t2)select * from temp03_cust_logon3

image-20240601181631768

方法三:lag 和 max 开窗函数

我使用 ‘0000-00-00’ 填充 NULL,lag 之后一个日期。再计算日期差,出现 NULL正好,不参与计算加减和判断。然后对日期差 date_diff 进行判断,是等于1,则判断成 0 ,如果不是1,则是登录日期 date ,为下一步做准备。最后使用 max() 开窗函数,逐项判断登录的最近(最大)日期。

“max(t1.date_line) over (partition by t1.kehu_id order by t1.date) as max_line” 意思是对 date_line 取最大值,按照客户号分区,登录日期 date 生序排序。

with temp02_cust_logon2 as
(selectt1.kehu_id,t1.date,row_number () over (partition by t1.kehu_id order by t1.date) as rn ,lag(t1.date,1,'0000-00-00') over (partition by t1.kehu_id order by t1.date asc) as lag_datefromtemp01_cust_logon as t1
)
-- select * from temp02_cust_logon2
,temp03_cust_logon3 as 
(selectt2.kehu_id,t2.date,t2.rn,t2.lag_date,date_diff(t2.date,t2.lag_date) as date_fiff,if (date_diff(t2.date,t2.lag_date) = 1,'0',t2.date) as date_linefromtemp02_cust_logon2 as t2)
selectt1.kehu_id,t1.date,t1.lag_date,t1.date_fiff,t1.date_line,max(t1.date_line) over (partition by t1.kehu_id order by t1.date) as max_line
fromtemp03_cust_logon3 as t1

image-20240601181722681

方法四:自相关

自相关理解相对容易,但是数据量大的话,产生的笛卡尔积,数据会爆炸性的增加,查询时间很久,不推荐数据量大的情况。截图数据不全。

使用客户号关联,第一个客户有8个日期,自关联后 2024-05-13 就会和自己另外的 8个日期关联到。这样是三个客户,分别有 8、4、14 个日期,那自相关后产生多行数据?276。是 8 * 8 + 4 * 4 + 14 * 14 = 276。

	selectt1.kehu_id,t1.date,t2.date as date2,t2.kehu_id as kehu_id2,date_sub(t1.date,2)  as date_subfromtemp01_cust_logon as t1inner jointemp01_cust_logon as t2on t1.kehu_id = t2.kehu_id

image-20240601181834819

selectt1.kehu_id,t1.date,t2.date as date2,t2.kehu_id as kehu_id2,date_sub(t1.date,2)  as date_subfromtemp01_cust_logon as t1inner jointemp01_cust_logon as t2on t1.kehu_id = t2. kehu_idwheret2.date between date_sub(t1.date,2) and t1.date 

date2 在 date_sub 和 date 之间。between and 是 >= and <= 。

image-20240601181943764

然后再统计。

with temp02_cust_logon2 as
(selectt1.kehu_id,t1.date,t2.date as date2,t2.kehu_id as kehu_id2,date_sub(t1.date,2)  as date_subfromtemp01_cust_logon as t1inner jointemp01_cust_logon as t2on t1.kehu_id = t2. kehu_idwheret2.date between date_sub(t1.date,2) and t1.date 
)select t1.kehu_id,t1.date, count(1) as cnt
from temp02_cust_logon2 as t1
group by t1.kehu_id,t1.date
havingcount(1)  >= 3

image-20240601182027509

小提示:Mac 操作excel重复上一步是 command + Y。替换的快捷键是command+shift+H,查找是 command + F

参考:

数仓面试——连续登录问题:https://mp.weixin.qq.com/s/W81ivF0uPWsVZP28IEhFvQ

这篇关于SQL面试题001--图文并茂解答连续登录问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1022098

相关文章

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

IDEA和GIT关于文件中LF和CRLF问题及解决

《IDEA和GIT关于文件中LF和CRLF问题及解决》文章总结:因IDEA默认使用CRLF换行符导致Shell脚本在Linux运行报错,需在编辑器和Git中统一为LF,通过调整Git的core.aut... 目录问题描述问题思考解决过程总结问题描述项目软件安装shell脚本上git仓库管理,但拉取后,上l

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)

mysql8.0.43使用InnoDB Cluster配置主从复制

《mysql8.0.43使用InnoDBCluster配置主从复制》本文主要介绍了mysql8.0.43使用InnoDBCluster配置主从复制,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录1、配置Hosts解析(所有服务器都要执行)2、安装mysql shell(所有服务器都要执行)3、

k8s中实现mysql主备过程详解

《k8s中实现mysql主备过程详解》文章讲解了在K8s中使用StatefulSet部署MySQL主备架构,包含NFS安装、storageClass配置、MySQL部署及同步检查步骤,确保主备数据一致... 目录一、k8s中实现mysql主备1.1 环境信息1.2 部署nfs-provisioner1.2.

idea npm install很慢问题及解决(nodejs)

《ideanpminstall很慢问题及解决(nodejs)》npm安装速度慢可通过配置国内镜像源(如淘宝)、清理缓存及切换工具解决,建议设置全局镜像(npmconfigsetregistryht... 目录idea npm install很慢(nodejs)配置国内镜像源清理缓存总结idea npm in

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

MySQL中VARCHAR和TEXT的区别小结

《MySQL中VARCHAR和TEXT的区别小结》MySQL中VARCHAR和TEXT用于存储字符串,VARCHAR可变长度存储在行内,适合短文本;TEXT存储在溢出页,适合大文本,下面就来具体的了解... 目录一、VARCHAR 和 TEXT 基本介绍1. VARCHAR2. TEXT二、VARCHAR

idea突然报错Malformed \uxxxx encoding问题及解决

《idea突然报错Malformeduxxxxencoding问题及解决》Maven项目在切换Git分支时报错,提示project元素为描述符根元素,解决方法:删除Maven仓库中的resolv... 目www.chinasem.cn录问题解决方式总结问题idea 上的 maven China编程项目突然报错,是

MySQL中C接口的实现

《MySQL中C接口的实现》本节内容介绍使用C/C++访问数据库,包括对数据库的增删查改操作,主要是学习一些接口的调用,具有一定的参考价值,感兴趣的可以了解一下... 目录准备mysql库使用mysql库编译文件官方API文档对象的创建和关闭链接数据库下达sql指令select语句前言:本节内容介绍使用C/