0

下面是一个示例表。

DECLARE @Temp TABLE (ID int, Name varchar(50), LiveDate Date, LiveTime time(7), Duration_Seconds int)

INSERT INTO @Temp (ID, Name, LiveDate, LiveTime, Duration)
SELECT 1, 'ABC', '2013-08-19', '00:01:00.0000000', 300
UNION ALL
SELECT 2, 'ABC', '2013-08-19', '00:01:00.0000000', 300
UNION ALL
SELECT 3, 'DEF', '2013-08-19', '00:01:00.0000000', 300
UNION ALL
SELECT 4, 'DEF', '2013-08-19', '00:03:00.0000000', 300
UNION ALL
SELECT 5, 'GHI', '2013-08-19', '00:01:00.0000000', 300
UNION ALL
SELECT 6, 'GHI', '2013-08-19', '00:01:00.0000000', 300
UNION ALL
SELECT 7, 'GHI', '2013-08-19', '00:03:00.0000000', 300
UNION ALL
SELECT 8, 'GHI', '2013-08-19', '00:09:00.0000000', 300
UNION ALL
SELECT 9, 'GHI', '2013-08-20', '00:06:00.0000000', 300
UNION ALL
SELECT 10, 'JKL', '2013-08-19', '00:01:00.0000000', 300
UNION ALL
SELECT 11, 'MNO', '2013-08-19', '00:01:00.0000000', 300

SELECT *,
    CASE
        WHEN COUNT(*) OVER (PARTITION BY  Name, LiveDate, LiveTime) > 1 THEN 1
        ELSE 0
    END AS Duplicate 
FROM @Temp

现在,我想要的输出如下。

/*
Desired Output
ID  Name    LiveDate        Livetime            Duration_Seconds    Duplicate   OverLap
1   ABC     2013-08-19      00:01:00.0000000    300                 Yes         No
2   ABC     2013-08-19      00:01:00.0000000    300                 Yes         No
3   DEF     2013-08-19      00:01:00.0000000    300                 No          Yes
4   DEF     2013-08-19      00:03:00.0000000    300                 No          Yes
5   GHI     2013-08-19      00:01:00.0000000    300                 Yes         Yes 
6   GHI     2013-08-19      00:01:00.0000000    300                 Yes         Yes
7   GHI     2013-08-19      00:03:00.0000000    300                 No          Yes
8   GHI     2013-08-19      00:09:00.0000000    300                 No          No
9   GHI     2013-08-20      00:06:00.0000000    300                 No          No
10  JKL     2013-08-19      00:01:00.0000000    300                 No          No
11  MNO     2013-08-19      00:01:00.0000000    300                 No          No
*/

我该怎么做呢?任何帮助,将不胜感激。

我不确定如何找到重叠。

对于重叠为 Yes/True/1,名称和日期必须相同。然后,我们必须查看时间和持续时间。假设对于 GHI,ID 5 和 6 的时间 = 12:01,ID 7 的时间 = 12:03。但根据持续时间,即 300 秒或 5 分钟,因为 12:03 在 12:01 的 5 分钟内,我想为这三个记录标记 Overlap = Yes/True/1。

将 LiveTime 视为开始时间。Duration_Seconds 作为记录的总时间。所以 GHI ID 5 & 6 LiveTime = 12:01 AM 并持续了 300 秒(5 分钟)。所以它在 12:01 AM 上线,在 12:06 AM 死亡。GHI ID 7 于上午 12:03 以相同的名称和日期上线。但它不应该有,因为我们已经记录了从 12:01 AM 到 12:06 AM 具有相同名称和日期的 Live。因此,所有 GHI 都标记为 Overlap = Yes/True/1

希望这可以帮助您了解我要做什么。

谢谢

4

1 回答 1

2

这将起作用,可能有更简单的方法:

;WITH cte AS (  SELECT *,
                    CASE
                        WHEN COUNT(*) OVER (PARTITION BY  Name, LiveDate, LiveTime) > 1 THEN 1
                        ELSE 0
                    END AS Duplicate 
                FROM @Temp)
SELECT DISTINCT a.*,CASE WHEN b.ID IS NOT NULL THEN 1 ELSE 0 END 'Overlap'
FROM  cte a
LEFT JOIN cte b
  ON   a.NAME = b.NAME
  AND a.LiveDate = b.LiveDate
  AND ((b.LiveTime > a.Livetime  AND b.LiveTime < DATEADD(SECOND,a.Duration_Seconds,a.LiveTime))
       OR (a.LiveTime > b.Livetime  AND a.LiveTime < DATEADD(SECOND,b.Duration_Seconds,b.LiveTime)))

JOIN如果以上不适用于所有重叠实例,您可能需要调整标准。

于 2013-08-19T22:23:08.340 回答