Access Query Grouping Dates

Posted on 2012-09-06
Last Modified: 2012-09-07
This isn't an easy task, but I was wondering if this is would be possible.

Below details is an example of a client service history. I need to somehow group the clients history into service periods.  It involves identifying break in consecutive days.

The reason why the client may exit and reenter on same day is because the entry and exit dates contain time stamps. Basically the person left the building to seek medical service and then returned.  

Client       Provider Id      Entry Date      Exit Date
1460      Engagement      7/13/2011      7/14/2011
1460      Engagement      7/14/2011      7/21/2011
1460      Engagement      7/21/2011      7/26/2011
1460      Engagement      7/26/2011      8/3/2011
1460      Engagement      8/8/2011                8/9/2011
1460      Engagement      8/9/2011               8/12/2011
1460      Engagement      8/12/2011      8/19/2011
1460      Engagement      10/7/2011      10/8/2011
1460      Engagement      10/8/2011      10/10/2011
1460      Engagement      10/11/2011      10/12/2011
1460      Engagement      10/13/2011      10/17/2011
1460      Engagement      10/17/2011      10/19/2011
1460      Engagement      10/19/2011      10/20/2011
1460      Engagement      10/20/2011      10/21/2011
1460      Engagement      10/21/2011      10/21/2011
1460      Engagement      10/22/2011      10/23/2011
1460      Engagement      10/23/2011      10/24/2011
1460      Engagement      10/24/2011      10/26/2011
1460      Engagement      10/26/2011      10/27/2011
1460      Engagement      10/27/2011      10/28/2011
1460      Engagement      10/28/2011      10/31/2011
1460      Engagement      11/9/2011      11/10/2011
1460      Engagement      12/14/2011      12/15/2011
1460      Engagement      12/15/2011      12/16/2011

Client       Provider Id      Entry Date      Exit Date
1460      Engagement      7/13/2011      8/03/2011
1460      Engagement      8/08/2011      8/19/2011
1460      Engagement      10/07/2011      10/31/2011
1460      Engagement      11/09/2011      11/10/2011
1460      Engagement      12/14/2011      12/16/2011
Question by:jbakestull
    LVL 48

    Expert Comment

    by:Gustav Brock
    That could be:

      [Provider Id],
      Min([Entry Date]) As EntryDate,
      Max([Exit Date]) As ExitDate
    Group By
      [Provider Id],
      Year([Entry Date]),
      Month([Entry Date]);

    LVL 77

    Expert Comment

    You're not going to do this in a query.  It will require a VBA procedure.  Is that an acceptable approach?
    LVL 47

    Expert Comment

    by:Dale Fye (Access MVP)
    Are you attempting to do this for a specific (short) time period for a single client, or for several years for multiple clients?

    Author Comment

    Sorry, went out to lunch with wife. We don't get to do that very often.

    Thanks for the responses, VBA is fine, for multiple clients over several years.
    LVL 48

    Expert Comment

    by:Gustav Brock
    As I assume your test data is representative - where the break in consecutive days happens between months - the query will work for multiple clients over several years.

    LVL 77

    Accepted Solution

    Have a look at the attached example...

    Data in tbl1
    Run the procedure GetRanges
    Results in tbl2

    Author Comment

    peter57r, I'm amazed. I will play with this over the weekend and get back with you. Thank you. You answered my question and did an awesome job. 500 points is really not enough.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now