返回顶部
首页 > 资讯 > 数据库 >用sql对含有时间段字段(起始时间、结束时间)的记录做并集处理
  • 948
分享到

用sql对含有时间段字段(起始时间、结束时间)的记录做并集处理

摘要

来自于一个基友的问题:他的博客同问题链接    sql时间段取并集、合并 https://blog.csdn.net/Seandba/article/details/105152412 问题:计算通道的总开放时长,只要有任意一个终

用sql对含有时间段字段(起始时间、结束时间)的记录做并集处理

来自于一个基友的问题:

他的博客同问题链接    sql时间段取并集、合并 https://blog.csdn.net/Seandba/article/details/105152412 

问题:计算通道的总开放时长,只要有任意一个终端开放通道就算开放,难点在于各种终端开放时间重叠包含

aa128bd9b772d921814e068c67d5e7f4

问题测试数据

--问题一、测试数据--计算总开放时长(小时)
TRUNCATE TABLE xcp;
insert into xcp values("1","A1",to_date("20200317 01:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 06:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("2","A1",to_date("20200317 01:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 06:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("2","A1",to_date("20200317 01:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 08:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("2","A1",to_date("20200317 02:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 07:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("2","A1",to_date("20200317 03:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 07:00:00","yyyymmdd hh24:mi:ss"));

insert into xcp values("2","A1",to_date("20200317 05:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 09:00:00","yyyymmdd hh24:mi:ss "));
insert into xcp values("3","A1",to_date("20200317 09:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 11:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("3","A1",to_date("20200317 12:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 13:00:00","yyyymmdd hh24:mi:ss"));

insert into xcp values("2","A1",to_date("20200317 14:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 19:00:00","yyyymmdd hh24:mi:ss "));
insert into xcp values("3","A1",to_date("20200317 16:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 19:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("3","A1",to_date("20200317 18:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 19:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("3","A1",to_date("20200317 18:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 21:00:00","yyyymmdd hh24:mi:ss"));
commit;

SELECT * FROM xcp;

image2

问题核心是求多条记录之间的并集操作 ,我写的sql如下

--问题1
WITH tmp1 AS (  --取所有时间节点
SELECT channel,BEGIN_TIME TIME FROM xcp
UNION SELECT channel,end_time FROM xcp
UNioN SELECT channel,MIN(begin_time) FROM xcp GROUP BY channel
UNION SELECT channel,MAX(end_time) FROM xcp GROUP BY channel),

tmp2 AS(--每个时间节点连接到下个节点  形成时间段
SELECT a.channel,a.time,LEAD(a.time,1) OVER(PARTITION BY a.channel ORDER BY a.time) nexttime
FROM tmp1 a),

tmp3 AS(--每个时间段取中值
SELECT b.channel,b.TIME,b.nexttime,(b.nexttime-b.time)/2+b.time midtime
FROM tmp2 b
WHERE b.nexttime IS NOT NULL),

tmp4 AS(--若中值处于原始记录中  则该段时间为通道开通时间 否则通道不开通
SELECT c.*,
CASE WHEN EXISTS (SELECT 1 FROM xcp o WHERE c.midtime BETWEEN o.begin_time AND o.end_time) THEN 1 ELSE 0 END *
(c.nexttime-c.time)*24 duration
FROM tmp3 c)

SELECT nvl(d.channel,"合计时长") 通道,d.TIME 开始时间,d.nexttime 结束时间,
SUM(duration) "通道开通时间(小时)" FROM tmp4 d
GROUP BY rollup((d.channel,d.TIME,d.nexttime))
ORDER BY 2;

09e02bd2c2d7e12249c24dc5380b754看着就很垃圾的sql,执行计划一定垃圾,记录以备后查询吧

原理是吧时间节点拿出来,对没两个时间节点之间的时间段,取中间值到原始记录表查询,如果是,这段时间就是属于并集后的,然后对并集后的记录求和


问题2:求17日的的通道开放时长

--问题2、测试数据--计算27号开放时长(小时)
TRUNCATE TABLE xcp;
insert into xcp values("13","A1",to_date("20200314 08:00:00","yyyymmdd hh24:mi:ss"),to_date("20200315 09:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("14","A1",to_date("20200317 08:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 09:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("15","A1",to_date("20200316 03:00:00","yyyymmdd hh24:mi:ss"),to_date("20200317 05:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("16","A1",to_date("20200317 08:00:00","yyyymmdd hh24:mi:ss"),to_date("20200318 10:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("17","A1",to_date("20200316 08:00:00","yyyymmdd hh24:mi:ss"),to_date("20200318 10:00:00","yyyymmdd hh24:mi:ss"));
insert into xcp values("18","A1",to_date("20200320 08:00:00","yyyymmdd hh24:mi:ss"),to_date("20200321 10:00:00","yyyymmdd hh24:mi:ss"));
commit;

SELECT * FROM xcp ORDER BY begin_time

image5sql如下:

----问题2
WITH tmp1 AS (  --取所有时间节点    取17号就加入17号0点和24点两个时间
SELECT channel,BEGIN_TIME TIME FROM xcp
UNION SELECT channel,end_time FROM xcp
UNION SELECT channel,MIN(begin_time) FROM xcp GROUP BY channel
UNION SELECT channel,MAX(end_time) FROM xcp GROUP BY channel
UNION SELECT DISTINCT channel,to_date("20200317","yyyymmdd") FROM xcp
UNION SELECT DISTINCT channel,to_date("20200318","yyyymmdd") FROM xcp),

tmp2 AS(--每个时间节点连接到下个节点  形成时间段
SELECT a.channel,a.time,LEAD(a.time,1) OVER(PARTITION BY a.channel ORDER BY a.time) nexttime
FROM tmp1 a),

tmp3 AS(--每个时间段取中值
SELECT b.channel,b.TIME,b.nexttime,(b.nexttime-b.time)/2+b.time midtime
FROM tmp2 b
WHERE b.nexttime IS NOT NULL
AND to_char(b.TIME,"yyyymmdd")=20200317),

tmp4 AS(--若中值处于原始记录中  则该段时间为通道开通时间 否则通道不开通
SELECT c.*,
CASE WHEN EXISTS (SELECT 1 FROM xcp o WHERE c.midtime BETWEEN o.begin_time AND o.end_time) THEN 1 ELSE 0 END *
(c.nexttime-c.time)*24 duration
FROM tmp3 c)

SELECT nvl(d.channel,"合计时长") 通道,d.TIME 开始时间,d.nexttime 结束时间,
SUM(duration) "通道开通时间(小时)" FROM tmp4 d
GROUP BY rollup((d.channel,d.TIME,d.nexttime))
ORDER BY 2;

image思路是在第一步取时间节点的时候单独加入17日0点24点的时间点即可





优化

上述代码全表扫描5次,效率垃圾,从小强的第8种情况的反面考虑,结合小强给的思路,即可优化到扫描一次全表即可,代码如下

  --第8的特征:下一条记录开始时间  大于  本条记录的结束时间;那么就把这部分时间记下来,最后减掉即可
WITH tmp AS(
SELECT a.channel,a.begin_time,a.end_time,
(LEAD(a.begin_time,1) OVER(PARTITION BY a.channel ORDER BY begin_time,end_time)  - a.end_time)*24 hoursto_next_begin_time  --距离下一条记录的时间间隔  如果是正数就是第8种情况
FROM xcp a) 

SELECT (MAX(end_time)-MIN(begin_time))*24 - sum(DECODE(sign(hoursto_next_begin_time),1,hoursto_next_begin_time,0))  通道开通时间
FROM tmp aa

image

您可能感兴趣的文档:

--结束END--

本文标题: 用sql对含有时间段字段(起始时间、结束时间)的记录做并集处理

本文链接: https://lsjlt.com/news/5210.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作