未加星标

Calculate overlap times for time overlaps in MySQL

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

i have a database table containing events:

CREATE TABLE events (event VARCHAR(32) ,down_time TIMESTAMP ,up_time TIMESTAMP ,id INT UNSIGNED NOT NULL AUTO_INCREMENT KEY ,INDEX(event(16)) ); INSERT INTO events(event, down_time, up_time) VALUES ('e1', '2015-01-01 00:00:03', '2015-01-01 00:00:08'), ('e2', '2015-01-01 00:00:05', '2015-01-01 00:00:06'), ('e3', '2015-01-01 00:00:02', '2015-01-01 00:00:09'), ('e4', '2015-01-01 00:00:01', '2015-01-01 00:00:04'), ('e5', '2015-01-01 00:00:07', '2015-01-01 00:00:10'); select * from events; | event | down_time | up_time | id | +-------+---------------------+---------------------+----+ | e1 | 2015-01-01 00:00:03 | 2015-01-01 00:00:08 | 1 | | e2 | 2015-01-01 00:00:05 | 2015-01-01 00:00:06 | 2 | | e3 | 2015-01-01 00:00:02 | 2015-01-01 00:00:09 | 3 | | e4 | 2015-01-01 00:00:01 | 2015-01-01 00:00:04 | 4 | | e5 | 2015-01-01 00:00:07 | 2015-01-01 00:00:10 | 5 |

I find event overlaps using the following query (query1):

SELECT * FROM events a JOIN events b ON a.down_time <= b.up_time AND a.up_time >= b.down_time WHERE a.id < b.id AND a.event != b.event AND a.event regexp 'e[1-5]' AND b.event regexp 'e[1-5]';

which produces the following event overlaps (results1):

| event | down_time | up_time | id | event | down_time | up_time | id | +-------+---------------------+---------------------+----+-------+---------------------+---------------------+----+ | e1 | 2015-01-01 00:00:03 | 2015-01-01 00:00:08 | 1 | e2 | 2015-01-01 00:00:05 | 2015-01-01 00:00:06 | 2 | | e1 | 2015-01-01 00:00:03 | 2015-01-01 00:00:08 | 1 | e3 | 2015-01-01 00:00:02 | 2015-01-01 00:00:09 | 3 | | e1 | 2015-01-01 00:00:03 | 2015-01-01 00:00:08 | 1 | e4 | 2015-01-01 00:00:01 | 2015-01-01 00:00:04 | 4 | | e1 | 2015-01-01 00:00:03 | 2015-01-01 00:00:08 | 1 | e5 | 2015-01-01 00:00:07 | 2015-01-01 00:00:10 | 5 | | e2 | 2015-01-01 00:00:05 | 2015-01-01 00:00:06 | 2 | e3 | 2015-01-01 00:00:02 | 2015-01-01 00:00:09 | 3 | | e3 | 2015-01-01 00:00:02 | 2015-01-01 00:00:09 | 3 | e4 | 2015-01-01 00:00:01 | 2015-01-01 00:00:04 | 4 | | e3 | 2015-01-01 00:00:02 | 2015-01-01 00:00:09 | 3 | e5 | 2015-01-01 00:00:07 | 2015-01-01 00:00:10 | 5 | +-------+---------------------+---------------------+----+-------+---------------------+---------------------+----+

I want to show overlap durations for each row in event overlaps (results1) and currently use the following conditional tests withinphp:

if (a.down_time <= b.down_time && b.up_time <= a.up_time) { overlap_duration = b.up_time-b.down_time; } else if (a.down_time >= b.down_time && a.up_time <= b.up_time) { overlap_duration = a.up_time-a.down_time; } else if (a.down_time <= b.down_time) { overlap_duration = a.up_time-b.down_time; } else if (a.down_time >= b.down_time) { overlap_duration = b.up_time-a.down_time; } 1 2 3 4 5 6 7 8 9 10 | | | | | | | | | | | | |----------e1-------| | | | | | | | e2| | | | | a.down_time <= b.down_time && b.up_time <= a.up_time | |------------e3-------------| | a.down_time >= b.down_time && a.up_time <= b.up_time |----e4-----| | | | | | | a.down_time >= b.down_time | | | | | | |-----e5----| a.down_time <= b.down_time

and then produce the following output (results2):

| event | down_time | up_time | duration | | e1 | 2015-01-01 00:00:03 2015-01-01 00:00:08 00:00:00:05 | | e2 | 2015-01-01 00:00:05 2015-01-01 00:00:06 00:00:00:01 | | Overlap1 | 2015-01-01 00:00:05 2015-01-01 00:00:06 00:00:00:01 | | | | | e1 | 2015-01-01 00:00:03 2015-01-01 00:00:08 00:00:00:05 | | e3 | 2015-01-01 00:00:02 2015-01-01 00:00:09 00:00:00:07 | | Overlap2 | 2015-01-01 00:00:03 2015-01-01 00:00:08 00:00:00:05 | | | | | e1 | 2015-01-01 00:00:03 2015-01-01 00:00:08 00:00:00:05 | | e4 | 2015-01-01 00:00:01 2015-01-01 00:00:04 00:00:00:03 | | Overlap3 | 2015-01-01 00:00:03 2015-01-01 00:00:04 00:00:00:01 | | | | | e1 | 2015-01-01 00:00:03 2015-01-01 00:00:08 00:00:00:05 | | e5 | 2015-01-01 00:00:07 2015-01-01 00:00:10 00:00:00:03 | | Overlap4 | 2015-01-01 00:00:07 2015-01-01 00:00:08 00:00:00:01 | | | | | e2 | 2015-01-01 00:00:05 2015-01-01 00:00:06 00:00:00:01 | | e3 | 2015-01-01 00:00:02 2015-01-01 00:00:09 00:00:00:07 | | Overlap5 | 2015-01-01 00:00:05 2015-01-01 00:00:06 00:00:00:01 | | | | | e3 | 2015-01-01 00:00:02 2015-01-01 00:00:09 00:00:00:07 | | e4 | 2015-01-01 00:00:01 2015-01-01 00:00:04 00:00:00:03 | | Overlap6 | 2015-01-01 00:00:02 2015-01-01 00:00:04 00:00:00:02 | | | | | e3 | 2015-01-01 00:00:02 2015-01-01 00:00:09 00:00:00:07 | | e5 | 2015-01-01 00:00:07 2015-01-01 00:00:10 00:00:00:03 | | Overlap7 | 2015-01-01 00:00:07 2015-01-01 00:00:09 00:00:00:02 | +-----------+-----------------------------------------------------------+

I suspect it may be better to calculate overlap durations within mysql by processing results1 to produce results2 but not sure how best to proceed .. if at all? Thanks in advance.

This should work as expected

SELECT a.*, b.*, (least(a.up_time, b.up_time) - greatest(a.down_time, b.down_time)) as overlap_seconds FROM events a JOIN events b ON a.down_time <= b.up_time AND a.up_time >= b.down_time WHERE a.id < b.id AND a.event != b.event AND a.event regexp 'e[1-5]' AND b.event regexp 'e[1-5]' GROUP BY a.id, b.id;

to get the overlap, all you need to do is compare the "smallest" up_time with the "biggest" down_time for each row ...

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

tags: time,down,up,event,lt,e1
分页:12
转载请注明
本文标题:Calculate overlap times for time overlaps in MySQL
本站链接:https://www.codesec.net/view/604974.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(12)