Marshall Barton wrote
[]
FROM timeclocks AS L INNER JOIN timeclocks AS R
ON L.FName = R.FName
AND L.RecordDate = R.RecordDate
AND L.StartTime < R.StartTime
WHERE L.StartTime < R.Stoptime
AND R.StartTime < L.Stoptime
ORDER BY L.FName, L.StartTime
Tim said:
On testing this, it doesn't get all the test cases; in particular when
two start times are identical (eg 12:00-14:00 and 12:00-13:00). This one
gets all the test cases that I can come up with:
SELECT L.FName, L.RecordDate,
L.StartTime AS start1, L.StopTime AS stop1,
R.StartTime AS start2, R.StopTime AS stop2
FROM timeclocks AS L INNER JOIN timeclocks AS R
ON L.FName = R.FName
AND L.RecordDate = R.RecordDate
AND L.StartTime <= R.StartTime
AND L.RecordID <> R.RecordID
WHERE R.StartTime < L.Stoptime
ORDER BY L.FName, L.StartTime
I don't think this misses any overlaps.
The only problem is that pairs with identical start times are listed
twice; and that when there are three or more overlaps each overlapping
pair is listed. It's probably possible to remove these with a bunch of
IIF clauses.
Alternatively, using a SELECT DISTINCT to reduce the output to provide
just FName and RecordDate also squashes the duplication (and is logically
more consistent).
Is the OP still with us?
Tim, I'm getting a headache looking for counter examples
here. Maybe your query works, but there is a lack of
symmetry that bothers me.
Copping out by bowing to someone smarter than me (see
http://allenbrowne.com/appevent.html), I think this may be a
correct paraphrase (ignoring the Null issues) of Allen's
query using a Join instead of a cartesian product (which are
probably the same thing by the time it gets to the db
engine).
SELECT L.FName, L.RecordDate,
L.StartTime AS start1, L.StopTime AS stop1,
R.StartTime AS start2, R.StopTime AS stop2
FROM timeclocks AS L INNER JOIN timeclocks AS R
ON L.FName = R.FName
AND L.RecordDate = R.RecordDate
AND L.RecordID <> R.RecordID
WHERE R.StartTime < L.Stoptime
AND L.StartTime < R.Stoptime
ORDER BY L.FName, L.RecordDate, L.StartTime
I really do not want to get into the issue of results being
returned twice since that strikes me as being a somewhat
existential question. After all, who's to say that interval
L conflicts with interval R instead of interval R
conflicting with interval L :-\