首页 > 数据库 >数仓sql场景:连续跟踪问题

数仓sql场景:连续跟踪问题

时间:2024-06-20 17:54:32浏览次数:13  
标签:数仓 场景 sql start 01 2023 date id select

1.需求

查询用户连续追踪客户的开始时间和结束时间,以下两种场景都需支持
第一种场景:
连续:中间没有断,如a追踪客户小明,第1次开始为2023-01-01,结束时间为:2023-01-03;第2次开始为2023-01-04,结束时间为:2023-01-07,则追踪时间为开始时间为2023-01-01,结束时间为:2023-01-07
不连续:中间有段,如a追踪客户小明,第1次开始为2023-01-01,结束时间为:2023-01-03;第2次开始为2023-01-06,结束时间为:2023-01-07,则追踪时间也为开始时间为2023-01-01,结束时间为:2023-01-07

2.应用场景

销售跟踪场景,电商关注收藏场景

3.示例数据

with a as
(select 'a' as id,'小明' as name,'2023-01-01' as start_date,'2023-01-03' as end_date 
union all
select 'a' as id,'小明' as name,'2023-01-04' as start_date,'2023-01-07' as end_date 
union all
select 'a' as id,'小明' as name,'2023-01-07' as start_date,'2023-01-11' as end_date 
union all
select 'b' as id,'小明' as name,'2023-01-12' as start_date,'2023-01-17' as end_date 
union all
select 'b' as id,'小明' as name,'2023-01-18' as start_date,'2023-01-21' as end_date 
union all
select 'b' as id,'小明' as name,'2023-01-22' as start_date,'2023-01-25' as end_date 
union all
select 'a' as id,'小明' as name,'2023-01-26' as start_date,'2023-01-30' as end_date
union all
select 'a' as id,'小明' as name,'2023-01-31' as start_date,'2023-02-03' as end_date)

4.sql实现

hivesql实现,在仓内实现结果直接应用

with a as
(select 'a' as id,'小明' as name,'2023-01-01' as start_date,'2023-01-03' as end_date 
union all
select 'a' as id,'小明' as name,'2023-01-04' as start_date,'2023-01-07' as end_date 
union all
select 'a' as id,'小明' as name,'2023-01-07' as start_date,'2023-01-11' as end_date 
union all
select 'b' as id,'小明' as name,'2023-01-12' as start_date,'2023-01-17' as end_date 
union all
select 'b' as id,'小明' as name,'2023-01-18' as start_date,'2023-01-21' as end_date 
union all
select 'b' as id,'小明' as name,'2023-01-22' as start_date,'2023-01-25' as end_date 
union all
select 'a' as id,'小明' as name,'2023-01-26' as start_date,'2023-01-30' as end_date
union all
select 'a' as id,'小明' as name,'2023-01-31' as start_date,'2023-02-03' as end_date)

select b.id,b.name,min(b.start_date),max(b.end_date) from 
(select *,row_number() over(order by start_date) -row_number() over(partition by id order by start_date) as rn from a) b group by b.id,b.name,b.rn;

连续查询结果:

不连续查询结果:

5.总结

这个场景题大部分数据开发者会因为连续和不连续的两个场景想复杂,需要多思考

标签:数仓,场景,sql,start,01,2023,date,id,select
From: https://www.cnblogs.com/jiashengmei/p/18259177

相关文章

  • postgresql简单使用
    postgresql.conf#系统、数据库配置文件pg_hha.conf#客户机连接文件pg_ctl和pgsql一、pg_ctl服务管理命令pg_ctl是PostgreSQL提供的控制服务启动、停止、重启等操作的命令行工具。常用选项和示例命令 选项 描述 示例启动PostgreSQL服务 start 启动数据库服......
  • SVG <pattern> 标签的用法和应用场景
    通过使用<pattern>标签,可以在SVG图像内部定义可重复使用的任意图案。这些图案可以通过fill属性或stroke属性进行引用。使用场景例如我们要在<svg>中绘制大量的圆点点,可以通过重复使用<circle>标签来实现。<svgwidth="100"height="100"xmlns="http://www.......
  • SVG <pattern> 标签的用法和应用场景
    通过使用<pattern>标签,可以在SVG图像内部定义可重复使用的任意图案。这些图案可以通过fill属性或stroke属性进行引用。使用场景例如我们要在<svg>中绘制大量的圆点点,可以通过重复使用<circle>标签来实现。<svgwidth="100"height="100"xmlns="http://www.w3.org......
  • 允许本地Sql Server 远程连接
    1.在Sqlserver上启用远程连接SqlServerManagemrntStudio中连接上数据库右键连接对象,选择"属性"=》连接 勾选允许远程连接2.如果使用TCP/IP协连接,要确保启用TCP/IP协议  右键其属性查看端口,一般默认是1433  注:重启Sqlserver服务才会生效 3.如果你的SqlSe......
  • TCP与UDP详解:层次、区别及应用场景
    TCP和UDP的层次及区别详解所属层次TCP(TransmissionControlProtocol,传输控制协议)和UDP(UserDatagramProtocol,用户数据报协议)都属于OSI模型中的传输层(第四层)。在传输层,协议的主要作用是为端到端的通信提供逻辑通信,并确保数据在网络上传输的可靠性和顺序。TCP和UDP的区别......
  • CentOS7系统搭建web环境 php&nginx&pgsql
    环境:VMware、CentOS-7-x86_64-DVD-2009.iso、nginx-1.26.1、php-7.2.0、postgresql-12php最好安装对应php项目所需版本,否则会出现不兼容问题。一、VMware安装CentOS7操作系统下载LinuxCentos7映像:https://mirrors.aliyun.com/centos/7/isos/x86_64/?spm=a2c6h.25603864.0.......
  • 详解Web应用安全系列(1)注入漏洞之SQL注入
    注入漏洞通常是指在可输入参数的地方,通过构造恶意代码,进而威胁应用安全和数据库安全。常见的注入漏洞包括:SQL注入和XSS跨站脚本攻击。这篇文章我们主要讲SQL注入,SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查......
  • MySQL计算两个地理坐标点之间的球面距离
    st_distance_sphere函数是mysql5.7提供的,可以直接查询两个经纬度之间相距多少米,它接受两个参数,每个参数是一个点的经度和纬度表结构:CREATETABLE`video_alarm`(`alarm_id`char(50)NOTNULL,`alarm_type`varchar(20)DEFAULTNULLCOMMENT'预警类型',`alarm_t......
  • 使用mysqlbackup备份工具加密备份
    1.生成keyecho-n"123456"|shasum-a2568d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92-#注意后面的中划线不算,共64个字符2.方式1:使用key#备份mysqlbackup--defaults-file=/etc/my.cnf--backup-dir=/tmp/fullbak--backup-image=/......
  • 索引失效的10个场景
    不满足最左匹配原则使用了select*索引列上有计算索引列用了函数字段类型不同like左边包含%列对比使用or关键字notin和notexistsorderby的坑建表CREATETABLE`user`(`id`intNOTNULLAUTO_INCREMENT,`code`varchar(20)C......