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

相关文章

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字