Results 1 to 8 of 8
  1. #1
    DavidZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    9

    How to query for overlapping dates

    Hi All,

    I am trying to find all Payees who have overlapping dates. It would look something like this:

    PayeeID: 3245


    1/1/2011 - 5/1/2014
    4/1/2013 - 4/1/2014
    4/2/2014 - 9/1/2014

    In this example, 5/1/2014 is incorrect and should be 3/31/2013.

    My SQL is as follows, but isn't returning anyone.

    SELECT EffectivePayee1.PayeeID
    FROM EffectivePayee1
    WHERE (((EffectivePayee1.Start_Date)>[EffectivePayee1].[End_Date] And (EffectivePayee1.Start_Date)<[EffectivePayee1].[End_Date]))

    Thoughts?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  3. #3
    DavidZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    9
    Thanks, orange!

    I changed it around and it worked. It is surprisingly hard to wrap my mind around why it works and the other way doesn't, though....

    The SQL that works is as follows:

    SELECT EffectivePayee1.PayeeID
    FROM EffectivePayee1
    WHERE (((EffectivePayee1.Start_Date)>[EffectivePayee1].[End_Date] And (EffectivePayee1.End_Date)<[EffectivePayee1].[Start_Date]));

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Are you sure that is doing what you think it does? If in your original post those are three different records for that employee, then the solution to your question becomes much more difficult.

    The SQL can only compare dates on one record at a time, not between records. And if you exclude equal values for A and B, then if A > B, obviously B < A - this is what your revised SQL is doing.

  5. #5
    DavidZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    9
    Hi John_G,

    No, I'm not sure that it is doing what I want it to do. The reason I initially thought this was the answer is simply because I returned records (versus not returning any the first time around).

    However, I was expecting to return a lot more than I did and I think this is because of what you mention above.

    Is there any way to compare dates between records?

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  7. #7
    DavidZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    9
    Whoops, hold on real quick.

  8. #8
    DavidZ is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2015
    Posts
    9
    Ok, this is how I got it to work in the end:

    SELECT DISTINCT EffectivePayee1.PayeeID
    FROM EffectivePayee1 INNER JOIN EffectivePayee2 ON EffectivePayee1.PayeeID=EffectivePayee2.PayeeID
    WHERE (EffectivePayee1.Start_Date BETWEEN EffectivePayee2.Start_Date AND EffectivePayee2.End_Date
    OR EffectivePayee1.End_Date BETWEEN EffectivePayee2.Start_Date AND EffectivePayee2.End_Date)
    AND EffectivePayee1.Start_Date<>EffectivePayee2.Start_ Date
    AND EffectivePayee1.End_Date<>EffectivePayee2.End_Date

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to visualise overlapping appointments
    By elpidoforos in forum Access
    Replies: 1
    Last Post: 09-30-2014, 03:59 PM
  2. Splitting and Suming Overlapping Data?
    By TinyRobot in forum Queries
    Replies: 3
    Last Post: 06-17-2014, 02:23 PM
  3. Queries with Overlapping Data
    By ineedaccesshelp in forum Queries
    Replies: 1
    Last Post: 11-28-2012, 11:48 AM
  4. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 PM
  5. Overlapping Subreports
    By gopherking in forum Reports
    Replies: 3
    Last Post: 11-09-2011, 07:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums