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

相关文章

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2