重叠通话时长

时间:2014-09-08 10:57:19

标签: sql sql-server overlap

如果呼叫与正在进行的其他呼叫重叠,是否有办法解决。

例如,我有一个10:00来电,10:05结束。

第二次调用来自10:02并以10:06结束,因此与第一次调用重叠,我怎么能在sql查询中显示它?我有几千个要比较的电话。

表I使用CallID, TimeAns, TimeFin

欢迎任何建议。

4 个答案:

答案 0 :(得分:0)

当呼叫1的开始或结束在呼叫2的开始和结束之间时,两个呼叫重叠。(无论如何,您呼叫的两个呼叫中的哪一个"呼叫1和#34;以及"呼叫2& #34;。)

WHERE 
(
  call1.start BETWEEN call2.start AND call2.end
  OR
  call1.end BETWEEN call2.start AND call2.end
)

编辑:如果您考虑两个呼叫,其中一个在另一个开始重叠或不同时结束的时候,请大家注意。 BETWEEN子句有。因此,如果您不想这样做,请将其替换为(call1.start > call2.start AND call1.start < call2.end)

编辑:查找所有重叠的呼叫组合:

select * 
from calls
join calls other 
  on other.callid < calls.callid and 
  (
    (calls.timeans > other.timeans and calls.timeans < other.timefin)
    or
    (calls.timefin > other.timeans and calls.timeans < other.timefin)
  )
;

答案 1 :(得分:0)

加入两个调用重叠的同一个表:

SELECT * FROM TABLE
JOIN TABLE t2 on t2.CallID != TABLE.CallID and 
    (CallID.TimeFin BETWEEN t2.TimeAns AND t2.TimeFin
    OR CallID.TimeAns BETWEEN t2.TimeAns AND t2.TimeFin)

答案 2 :(得分:0)

查询可以帮助您:

SELECT A.callID ,
A.startTime OVERLAPPING_START_TIME, 
A.endTime OVERLAPPING_END_TIME
    FROM phoneCall A 
         LEFT OUTER JOIN 
         phoneCall B 
   ON A.callID != B.callID
WHERE
      ((B.startTime) BETWEEN (A.STARTTIME) AND (A.endTime ))
OR 
      ((B.endTime ) BETWEEN (A.STARTTIME) AND (A.endTime ))

答案 3 :(得分:0)

以下是非包含性查询:

SELECT * FROM myCalls t1
INNER JOIN myCalls t2 on t2.CallID != t1.CallID 
    AND t1.TimeAns < t2.TimeFin
    AND t1.TimeFin >  t2.TimeAns 

此查询将为每个重叠返回2行。如果你想每次重叠只有一行,那么就可以了:

SELECT * FROM myCalls t1
INNER JOIN myCalls t2 on t2.CallID > t1.CallID 
    AND t1.TimeAns < t2.TimeFin
    AND t1.TimeFin >  t2.TimeAns