Re: Find Records that ovelap

From: Gary Walter (garylwpleasenospam_at_wamego.net)
Date: 05/25/04


Date: Tue, 25 May 2004 06:51:28 -0500


"John H" wrote
> I have a table with 2 date fields startdate and enddate. They vaules are long int
in yyyymmdd format.
>
> I want to check for any overlap between records.
>
> Eg if record 1 starts on 20040501 and ends on 20040530 and record 2 starts on
20040515 there is overlap between these records.
>
Hi John,

Michel once explained overlap so succinctly,
I quote him here:

Ranges do NOT overlap if
 ( I assume aStart, aEnd, bStart and bEnd are the intervals):

    aStart > bEnd OR aEnd < bStart

they overlap, in part or in full, on the negation of that statement, ie
(Apply De Morgan's law) :

    aStart <= bEnd AND aEnd >= bStart

<end quote>

If I understand correctly, you want a query that will
look at all the records in the table and find the ones
that overlap.

One possible method might be to bring the table
twice into your query,

set alias of one to "a", the other to "b",

no join (Cartesian join),

bring a.StartDate, a.EndDate, b.StartDate, b.EndDate
down into field rows of grid,

then use above in WHERE clause
(but maybe also add clause to weed out where they
are both equal, maybe you won't need this)

SELECT
a.StartDate,
a.EndDate,
b.StartDate,
b.EndDate
FROM yourtable AS a, yourtable AS b
WHERE
(a.StartDate <= b.EndDate
AND
a.EndDate >= b.StartDate)
AND
(a.StartDate <> b.StartDate
AND
a.EndDate <> b.EndDate);

This is untested, but hopefully this will get
you started if something not quite right.
Of course you can add other fields from
your a and b table.

Good luck,

Gary Walter



Relevant Pages

  • Re: dynamic structure for storing/querying intervals
    ... intervals on a line (or, in possibly degenerate cases, just points). ... I'd like to be able to query the structure by providing a range (again, ... that when many intervals overlap the performance degrades significantly. ... Each node also contains the maximum endpoint ...
    (comp.programming)
  • Re: dynamic structure for storing/querying intervals
    ... intervals on a line (or, in possibly degenerate cases, just points). ... I'd like to be able to query the structure by providing a range (again, ... that when many intervals overlap the performance degrades significantly. ... Each node also contains the maximum endpoint ...
    (comp.programming)
  • Re: Availability between dates
    ... The interval does not overlap at all the interval [fromThis, ... make a second query which will look at all the items not in the first set. ... ID and belonging to a category, e.g. seats, chairs, tables, etc ... that will return which products can be hired based on the hire request ...
    (microsoft.public.access.queries)
  • Re: Date range overlap
    ... This will provide excellent support for my ... way the query works. ... Is the overlap a weekday? ... rather than allenbrowne at mvps dot org. ...
    (microsoft.public.access.formscoding)
  • Re: Reservation Query
    ... you are looking to see if there is an overlap between two date ... I'm building an Access DB that houses reservation information for a hotel. ... Each record in the reservation master table has a CHECK_IN date field and ... I want to build a search query that allows the user to see which rooms are ...
    (microsoft.public.access.queries)