如何汇总多个表的重叠时间范围

时间:2019-03-28 14:19:56

标签: sql sql-server

我有多个具有多个时间范围的表,我需要计算所有表重叠时的重叠时间,否则就不会计算

在下面的示例中,我只需要对带有边框的时间范围求和:

enter image description here

由于A并非总是<或> X等,因此不能仅使用基本theta联接。

示例表:

PLACE 1 PLACE 1 TIME IN     PLACE 1 TIME OUT    PLACE 2 PLACE 2 TIME IN     PLACE 2 TIME OUT    PLACE 3 PLACE 3 TIME IN     PLACE 3 TIME OUT
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 07:06:00.000 2013-05-09 08:24:00.000 3931    2013-05-09 11:04:00.000 2013-05-09 12:16:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 07:06:00.000 2013-05-09 08:24:00.000 3931    2013-05-09 08:49:00.000 2013-05-09 09:36:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 07:06:00.000 2013-05-09 08:24:00.000 3931    2013-05-09 07:52:00.000 2013-05-09 08:21:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 07:06:00.000 2013-05-09 08:24:00.000 3931    2013-05-09 09:57:00.000 2013-05-09 10:39:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 15:06:00.000 2013-05-09 15:39:00.000 3931    2013-05-09 11:04:00.000 2013-05-09 12:16:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 15:06:00.000 2013-05-09 15:39:00.000 3931    2013-05-09 08:49:00.000 2013-05-09 09:36:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 15:06:00.000 2013-05-09 15:39:00.000 3931    2013-05-09 07:52:00.000 2013-05-09 08:21:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 15:06:00.000 2013-05-09 15:39:00.000 3931    2013-05-09 09:57:00.000 2013-05-09 10:39:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 11:22:00.000 2013-05-09 12:31:00.000 3931    2013-05-09 11:04:00.000 2013-05-09 12:16:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 11:22:00.000 2013-05-09 12:31:00.000 3931    2013-05-09 08:49:00.000 2013-05-09 09:36:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 11:22:00.000 2013-05-09 12:31:00.000 3931    2013-05-09 07:52:00.000 2013-05-09 08:21:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 11:22:00.000 2013-05-09 12:31:00.000 3931    2013-05-09 09:57:00.000 2013-05-09 10:39:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 12:52:00.000 2013-05-09 14:35:00.000 3931    2013-05-09 11:04:00.000 2013-05-09 12:16:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 08:54:00.000 2013-05-09 11:02:00.000 3931    2013-05-09 11:04:00.000 2013-05-09 12:16:00.000
3929    2013-05-09 07:06:00.000 2013-05-09 08:29:00.000 3930    2013-05-09 12:52:00.000 2013-05-09 14:35:00.000 3931    2013-05-09 08:49:00.000 2013-05-09 09:36:00.000

1 个答案:

答案 0 :(得分:0)

好的,这是一个两步过程。首先,您要查找其他表中确实有重叠行的行。

用于查找两个范围之间重叠的古老技术是,仅当范围A的起点不在范围B的终点之后且范围A的终点不在范围B的起点之前,才可以存在重叠

换句话说,如果B在A结束之前开始,并且B在A结束之后也结束,则存在重叠。

现在如何在SQL中表达这一点:从“根”表开始,作为FROM子句中的第一个表。我们将其命名为Table1,其中的A和B列(来自您的图片)。

要在其他两个表中查找任何重叠的行,请像这样JOIN:

rockteer rollback

通过使用INNER JOIN,可以将所有三个表中没有重叠的所有行排除在考虑范围之外。

然后仅返回重叠的时间段,将FROM Table1 t1 INNER JOIN Table2 t2 ON t2.E <= t1.B AND t2.F >= t1.A INNER JOIN Table3 t3 --this table has to overlap both of the others ON t3.X <= t1.B AND t3.Y >= t1.A AND t3.X <= t2.F AND t3.Y >= t2.E 的MAX作为重叠的开始,将t1.A, t2.E, t3.X的MIN作为重叠的结束。