重叠时间戳的SQL查询

重叠时间戳的SQL查询,sql,vertica,Sql,Vertica,我有多行的开始时间和结束时间列彼此重叠 我需要使用SQL查找不同的时间间隔 样本数据: (6 -> 7) (6.30 -> 6.45) (8 -> 9) (8.30 -> 9.30) 输出: (6 -> 7) (8 -> 9.30) Vertica具有非常强大的“时间序列”和“条件事件”分析功能。你的问题可以通过这种方式很容易地解决 假设这是您的起始表: SQL> select * from otest ; t1 |

我有多行的开始时间和结束时间列彼此重叠

我需要使用SQL查找不同的时间间隔

样本数据:

(6 -> 7)
(6.30 -> 6.45)
(8 -> 9)
(8.30 -> 9.30)
输出:

(6 -> 7)
(8 -> 9.30)
Vertica具有非常强大的“时间序列”和“条件事件”分析功能。你的问题可以通过这种方式很容易地解决

假设这是您的起始表:

SQL> select * from otest ;
        t1          |         t2          
--------------------+--------------------
2016-03-04 06:00:00 | 2016-03-04 07:00:00
2016-03-04 06:30:00 | 2016-03-04 06:45:00
2016-03-04 08:00:00 | 2016-03-04 09:00:00
2016-03-04 08:30:00 | 2016-03-04 09:30:00
(4 rows)
其中,
t1
是您的开始时间戳
t2
是您的结束时间戳。你所要做的就是:

SQL> select 
         min(a.t1), 
         max(a.t2) 
     from ( 
         select 
            t1, 
            t2, 
            conditional_true_event ( t1 >= lag(t2) ) 
               over ( order by t1 ) as cte 
         from otest ) a  
     group by cte 
     order by 1 ;

        min         |         max         
--------------------+--------------------
2016-03-04 06:00:00 | 2016-03-04 07:00:00
2016-03-04 08:00:00 | 2016-03-04 09:30:00
(2 rows)
Vertica具有非常强大的“时间序列”和“条件事件”分析功能。你的问题可以通过这种方式很容易地解决

假设这是您的起始表:

SQL> select * from otest ;
        t1          |         t2          
--------------------+--------------------
2016-03-04 06:00:00 | 2016-03-04 07:00:00
2016-03-04 06:30:00 | 2016-03-04 06:45:00
2016-03-04 08:00:00 | 2016-03-04 09:00:00
2016-03-04 08:30:00 | 2016-03-04 09:30:00
(4 rows)
其中,
t1
是您的开始时间戳
t2
是您的结束时间戳。你所要做的就是:

SQL> select 
         min(a.t1), 
         max(a.t2) 
     from ( 
         select 
            t1, 
            t2, 
            conditional_true_event ( t1 >= lag(t2) ) 
               over ( order by t1 ) as cte 
         from otest ) a  
     group by cte 
     order by 1 ;

        min         |         max         
--------------------+--------------------
2016-03-04 06:00:00 | 2016-03-04 07:00:00
2016-03-04 08:00:00 | 2016-03-04 09:30:00
(2 rows)

我会评论毛罗的,但我没有代表。不幸的是,他的回答没有考虑当你有超过2个重叠周期时会发生什么。

以下是我的解决方案:

--create the table for the purposes of this demo
drop schema if exists TEST1 cascade;     
create schema if not exists TEST1;
drop table if exists      TEST1.otest;
create table if not exists TEST1.otest(t1 datetime, t2 datetime);

--create some example data
--example where 2nd period is entirely inside the first
insert into TEST1.otest(t1, t2) select '2016-03-04 06:00:00' ,'2016-03-04 07:00:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 06:30:00' ,'2016-03-04 06:45:00';

--example of multiple consecutive periods
insert into TEST1.otest(t1, t2) select '2016-03-04 08:00:00' ,'2016-03-04 09:00:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:15:00' ,'2016-03-04 08:25:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:26:00' ,'2016-03-04 08:27:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:28:00' ,'2016-03-04 08:29:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:30:00' ,'2016-03-04 09:30:00';

--example of another overlapping period extending the end time
insert into TEST1.otest(t1, t2) select '2016-03-04 10:00:00' ,'2016-03-04 10:30:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 10:15:00' ,'2016-03-04 10:45:00';

--query syntax
with i as (select * from TEST1.otest) 
,i2 as (select * ,max(t2) over (order by t1) as maxT2 from i)
,i3 as (select *, lag(i2.maxT2) over (order by t1) as laggedMaxT2 from i2)
,i4 as (select *, conditional_true_event(i3.t1 > i3.laggedMaxT2) over (order by t1) as grouper from i3)
select min(t1) as collapsedT1, max(t2) as collapsedT2 from i4 group by grouper
order by collapsedT1;

--results
    collapsedT1         |collapsedT2         |
--------------------|--------------------|
2016-03-04 06:00:00 |2016-03-04 07:00:00 |
2016-03-04 08:00:00 |2016-03-04 09:30:00 |
2016-03-04 10:00:00 |2016-03-04 10:45:00 |

编辑:如果您的数据按其他列分类,请记住将分区子句添加到最大值,条件条件和滞后分析,或者你会得到不确定的结果。

< P>我会评论毛罗的,但是我没有代表。不幸的是,他的回答不考虑当你有超过2个重叠周期时会发生什么。

以下是我的解决方案:

--create the table for the purposes of this demo
drop schema if exists TEST1 cascade;     
create schema if not exists TEST1;
drop table if exists      TEST1.otest;
create table if not exists TEST1.otest(t1 datetime, t2 datetime);

--create some example data
--example where 2nd period is entirely inside the first
insert into TEST1.otest(t1, t2) select '2016-03-04 06:00:00' ,'2016-03-04 07:00:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 06:30:00' ,'2016-03-04 06:45:00';

--example of multiple consecutive periods
insert into TEST1.otest(t1, t2) select '2016-03-04 08:00:00' ,'2016-03-04 09:00:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:15:00' ,'2016-03-04 08:25:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:26:00' ,'2016-03-04 08:27:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:28:00' ,'2016-03-04 08:29:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 08:30:00' ,'2016-03-04 09:30:00';

--example of another overlapping period extending the end time
insert into TEST1.otest(t1, t2) select '2016-03-04 10:00:00' ,'2016-03-04 10:30:00';
insert into TEST1.otest(t1, t2) select '2016-03-04 10:15:00' ,'2016-03-04 10:45:00';

--query syntax
with i as (select * from TEST1.otest) 
,i2 as (select * ,max(t2) over (order by t1) as maxT2 from i)
,i3 as (select *, lag(i2.maxT2) over (order by t1) as laggedMaxT2 from i2)
,i4 as (select *, conditional_true_event(i3.t1 > i3.laggedMaxT2) over (order by t1) as grouper from i3)
select min(t1) as collapsedT1, max(t2) as collapsedT2 from i4 group by grouper
order by collapsedT1;

--results
    collapsedT1         |collapsedT2         |
--------------------|--------------------|
2016-03-04 06:00:00 |2016-03-04 07:00:00 |
2016-03-04 08:00:00 |2016-03-04 09:30:00 |
2016-03-04 10:00:00 |2016-03-04 10:45:00 |

编辑:如果您的数据按其他列分类,请记住将分区子句添加到max、conditional\u true\u事件和lag analytics中,否则您可能会得到不确定的结果。

什么dbms?时间戳是如何存储的?Vertica db。时间戳格式的开始时间/结束时间:2016-03-03 14:19:05这是一个缺口和岛屿问题,已经回答了好几次了,因此:Vertica支持分析功能,因此修改其中一个答案应该很容易……Hi@dnoeth我找到了两个答案(参考-),但没有一个能解决我的问题。你能帮我举一个具体的例子吗?看看伊齐克·本·甘的SQL Server解决方案目前网站似乎有问题,这是以前的一个,这是我为Teradata发布的另一个什么dbms?时间戳是如何存储的?Vertica db。时间戳格式的开始时间/结束时间:2016-03-03 14:19:05这是一个缺口和岛屿问题,已经回答了好几次了,因此:Vertica支持分析功能,因此修改其中一个答案应该很容易……Hi@dnoeth我找到了两个答案(参考-),但没有一个能解决我的问题。你能帮我举一个具体的例子吗?看看伊齐克·本·甘的SQL Server解决方案网站目前似乎有问题,这是以前的一个,这是我为Teradata发布的另一个