使用SQL查找时间重叠

使用SQL查找时间重叠,sql,datetime,sqlite,Sql,Datetime,Sqlite,我有一个由一系列时间数据组成的表,如下所示: +--------+--------------------+---------------------+ | person | start | end | +--------+--------------------+---------------------+ | a |2014-12-04 13:00:00 | 2014-12-04 14:00:00 | | a |2

我有一个由一系列时间数据组成的表,如下所示:

+--------+--------------------+---------------------+
| person | start              | end                 |
+--------+--------------------+---------------------+
| a      |2014-12-04 13:00:00 | 2014-12-04 14:00:00 |
| a      |2014-12-04 13:30:00 | 2014-12-04 14:30:00 |
| b      |2014-12-04 13:00:00 | 2014-12-04 14:00:00 |
| b      |2014-12-04 13:30:00 | 2014-12-04 14:30:00 |
| b      |2014-12-04 14:00:00 | 2014-12-04 15:00:00 |
| a      |2014-12-04 19:00:00 | 2014-12-04 20:00:00 |
| a      |2014-12-04 19:30:00 | 2014-12-04 20:30:00 |
+--------+--------------------+---------------------+
+--------+--------------------+---------------------+
| person | start              | end                 |
+--------+--------------------+---------------------+
| a      |2014-12-04 13:00:00 | 2014-12-04 14:30:00 |
| b      |2014-12-04 13:00:00 | 2014-12-04 15:00:00 |
| a      |2014-12-04 19:00:00 | 2014-12-04 20:30:00 |
+--------+--------------------+---------------------+
我希望找到时间重叠并输出以下内容:

+--------+--------------------+---------------------+
| person | start              | end                 |
+--------+--------------------+---------------------+
| a      |2014-12-04 13:00:00 | 2014-12-04 14:00:00 |
| a      |2014-12-04 13:30:00 | 2014-12-04 14:30:00 |
| b      |2014-12-04 13:00:00 | 2014-12-04 14:00:00 |
| b      |2014-12-04 13:30:00 | 2014-12-04 14:30:00 |
| b      |2014-12-04 14:00:00 | 2014-12-04 15:00:00 |
| a      |2014-12-04 19:00:00 | 2014-12-04 20:00:00 |
| a      |2014-12-04 19:30:00 | 2014-12-04 20:30:00 |
+--------+--------------------+---------------------+
+--------+--------------------+---------------------+
| person | start              | end                 |
+--------+--------------------+---------------------+
| a      |2014-12-04 13:00:00 | 2014-12-04 14:30:00 |
| b      |2014-12-04 13:00:00 | 2014-12-04 15:00:00 |
| a      |2014-12-04 19:00:00 | 2014-12-04 20:30:00 |
+--------+--------------------+---------------------+
因此,我正在寻找一个查询,以查找时间间隔重叠的实例,我希望看到时间间隔所涉及的人,以及最早的时间段和最晚的时间段


很难解释,但我希望你得到jist

这相当棘手,但如果您有一个支持RECURSIVE的最新版本的sqlite在sqlfiddle.com上不起作用,那么这是可能的:

create table intervals (
  person text,
  start timestamp,
  end timestamp
);

insert into intervals values ('a', '2014-12-04 13:00:00', '2014-12-04 14:00:00');
insert into intervals values ('a', '2014-12-04 13:30:00', '2014-12-04 14:30:00');
insert into intervals values ('b', '2014-12-04 13:00:00', '2014-12-04 14:00:00');
insert into intervals values ('b', '2014-12-04 13:30:00', '2014-12-04 14:30:00');
insert into intervals values ('b', '2014-12-04 14:00:00', '2014-12-04 15:00:00');
insert into intervals values ('a', '2014-12-04 19:00:00', '2014-12-04 20:00:00');
insert into intervals values ('a', '2014-12-04 19:30:00', '2014-12-04 20:30:00');

WITH RECURSIVE
 merged_intervals(person, start, end) AS (
    select person, start, end
    from intervals

    where not exists (
        select * from intervals i2 
        where i1.person=i2.person
        and (i2.start<i1.start and i2.end>=i1.start)
    )

 UNION ALL
    select i1.person, i1.start, i2.end
    from merged_intervals i1, intervals i2 
    where i1.start<=i2.start and i1.end>i2.start and i1.end<i2.end
    and i1.person=i2.person
)
select * from merged_intervals i1
where not exists (
    select * from merged_intervals i2 
    where i1.person=i2.person
    and (
        (i2.start<i1.start and i2.end>i1.start)
        or
        (i2.end>i1.end and i2.start<i1.end)
        )
    )
order by start, person
;

您使用的是哪种数据库管理系统?博士后?甲骨文?这似乎有效,尽管我不能说我完全理解它!。我运行的是SQLite版本3.7.16.2,但必须升级到最新版本才能运行。如果有人感兴趣,可以在“我遇到了一个问题”中添加对WITH子句的支持。这对示例数据非常有效,但当我尝试在实际数据库(其中有5500多行)上运行它时,我得到了以下消息:数据库或磁盘已满,顺便说一下,磁盘未满!它有20GB的免费空间。有什么想法吗?我添加了一个where子句,可以提高性能,告诉我它是否解决了您的pb问题,以及它是否仍然有效:-