问题
Is there a way to work out if a call overlaps another call already in progress.
For example I have a call that comes in at 10:00
and ends at 10:05
.
Second call comes in at 10:02
and ends at 10:06
so is overlapping the first calls, how could I show this in a sql query? I have a few thousand calls to compare.
Table I have uses CallID, TimeAns, TimeFin
Any suggestions welcome.
回答1:
Here is non-inclusive query:
SELECT * FROM myCalls t1
INNER JOIN myCalls t2 on t2.CallID != t1.CallID
AND t1.TimeAns < t2.TimeFin
AND t1.TimeFin > t2.TimeAns
This query will return 2 rows for each overlap. if you want to have just one row per overlap this will do it:
SELECT * FROM myCalls t1
INNER JOIN myCalls t2 on t2.CallID > t1.CallID
AND t1.TimeAns < t2.TimeFin
AND t1.TimeFin > t2.TimeAns
回答2:
Join on the same table where the two calls overlap:
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)
回答3:
Two calls overlap when start or end of call 1 is between start and end of call 2. (No matter, which of the two calls you call "call 1" and which "call 2".)
WHERE
(
call1.start BETWEEN call2.start AND call2.end
OR
call1.end BETWEEN call2.start AND call2.end
)
EDIT: And make your mind up, if you consider two calls where one ends at the same time as the other starts overlapping or not. The BETWEEN clause does. So if you don't want this, replace this by (call1.start > call2.start AND call1.start < call2.end)
.
EDIT: To find all overlapping call combinations:
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)
)
;
回答4:
- Refer Working ORACLE Fiddle Here:
- Refer Working SQL SERVER Fiddle Here:
The Query should help you:
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 ))
来源:https://stackoverflow.com/questions/25722694/overlapping-call-durations