hivesql连续日期统计最大逾期/未逾期案例

1、虚表(测试表和数据) create test_table as

select a.cust_no, a.r_date, a.yqts from (

select '123' as cust_no, '20231101' as r_date, 0 as yqts

union all

select '123' as cust_no, '20231102' as r_date, 1 as yqts

union all

select '123' as cust_no, '20231103' as r_date, 2 as yqts

union all

select '123' as cust_no, '20231104' as r_date, 3 as yqts

union all

select '123' as cust_no, '20231105' as r_date, 0 as yqts

union all

select '123' as cust_no, '20231106' as r_date, 0 as yqts

union all

select '123' as cust_no, '20231107' as r_date, 1 as yqts

) a

2、加入有表test_table,数据如上,0标识未逾期,1标识逾期,求连续最大无逾期和逾期的天数,以上数据可直接看出最大连续逾期天数为3、未逾期未2。

3、首先将测试数据排序,让数据连续

select a.cust_no, a.r_date, a.yqts from test_table a where 1=1 order by a.cust_no, a.r_date asc

4、将数据分组并给是否逾期打上标识1逾期,2未逾期,多行转一行

select tx.cust_no, count(distinct tx.r_date) as sum_ctn, concat_ws('',collection_list(case when cast(tx.yqts as double) > 0 then '1' else '2' end)) as sfflag from (

select a.cust_no, a.r_date, a.yqts from test_table a where 1=1 order by a.cust_no, a.r_date asc

) tx

执行结果:

cust_no sum_ctn sfflag

123 7 2111221

5、使用正则表达式分别替换,2111221改字符串中包含逾期和未逾期,将逾期1*都用A替换掉(同时也是分割符),剩下就是未逾期的,逾期的同理

select ty.cust_no, ty.sum_ctn, regexp_replace(ty.sfflag, '\2+', 'A') as yqflag, regexp_replace(ty.sfflag, '\1+', 'A') as wyqflag from (

'123' as cust_no 7 as sum_ctn '2111221' as sfflag

) ty

执行结果:

cust_no sum_ctn yqflag wyqflag

123 7 A111A1 2A22A

6、使用炸裂函数explode结合lateral view将数据拆分未多行,使用一次lateral view会生成一次虚表

select tz.cust_no, max(sum_ctn) as sum_ctn, max(length(yqlength)) as yqlength, max(length(wyqlength)) as wyqlength from (

123 as cust_no 7 as sum_ctn A111A1 as yqflag 2A22A as wyqflag

) tz

lateral view explode(split(yqflag,'A')) tb1 as yqlength

lateral view explode(split(wyqflag,'A')) tb2 as wyqlength

group by tz.cust_no

相关推荐
镰刀韭菜9 小时前
用 AI 做数据分析时,怎么保住自己的思考力
sql·数据分析·学习方法·提示工程·ai应用
Databend9 小时前
用 SQL 排查支付故障,OpenTelemetry 接入 Databend Cloud 实战
大数据·数据库·sql
AI源码16 小时前
基于Spark的期刊推荐系统的设计与实现
大数据·分布式·spark
AI源码17 小时前
基于 Hadoop 和 Hive 的旅游景区数据的分析与可视化系统
hive·hadoop·旅游
Decay__1 天前
SQLServer 运维自动化平台 —— PowerShell + SQL Agent 作业:部署、监控、巡检、告警、工单闭环一体化
运维·数据库·sql·mysql·架构
星恒随风1 天前
MySQL 从零入门(十):MVCC、Undo Log 与 Read View 详解——InnoDB 如何实现事务隔离?
linux·数据库·笔记·sql·mysql
Decay__2 天前
附 - 02・SQLServer 分库分表与中间件 —— 原生分区表、横向分库实践与联邦查询
运维·数据库·sql·mysql·架构
Decay__2 天前
SQLServer 优化案例集 —— 慢 SQL 优化、索引重构与参数调整收益对比
运维·数据库·sql·mysql·架构
codeejun3 天前
每日一Go·MySQL-15、慢查询日志实战分析(从 0 到 1 实战排查慢 SQL)
sql·mysql·golang
Decay__4 天前
SQLServer 监控体系构建 —— sql_exporter + Prometheus + Grafana 全栈实战
运维·数据库·sql·mysql·架构