Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2018
    Posts
    2

    Calculate days between dates in 2 rows in the same table

    [Solved]



    I have a table tblGroupHistory, with the fields pID, gID, ghStartDate, ghComment. (pID refers to tblPerson, gID to tblGroup).
    I would like to have a query with the number of days in a group, preferably with also the ghComment of both records.

    Example:
    Code:
    pID  gID  ghStartDate  ghComment
     1    3    02/20/2018  excellent performance
     2    6    02/23/2018
     1    2    02/28/2018  group 3 was to much
    Preferable result:
    Code:
    pID  gID  StartDate      EndDate        Days  CommentStart              CommentEnd
      1   3   02/20/2018     02/28/2018       8   excellent performance     group 3 was to much
      2   6   02/23/2018
      1   2   02/28/2018                          group 3 was to much
    (but a query that results in pID, gID, ghStartDate, ghComment, Days helps already a lot)

    Can anyone help me with this?
    Last edited by Jan de Zoveelste; 09-27-2018 at 09:14 AM.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    1 row cannot see another row, so, you need a few queries:
    Q1 pull rec 1,
    Q2, pull the record ID using MAX(id) where ID < Q1.ID
    Q3, put Q1 & Q2 together

  3. #3
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    It can also be done fairly easily using a subquery.
    See the section 'Get Value from another record' in this link http://allenbrowne.com/subquery-01.html
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  4. #4
    Join Date
    Sep 2018
    Posts
    2
    I've solved it with ridders52s link.

    Thank you both for your help!

  5. #5
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    You're welcome
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

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

Similar Threads

  1. How can I calculate working days between two dates?
    By lleeman in forum Programming
    Replies: 11
    Last Post: 03-21-2019, 03:54 PM
  2. How To Calculate Days Between Dates
    By DigitalAdrenaline in forum Access
    Replies: 3
    Last Post: 08-22-2016, 10:08 PM
  3. Replies: 5
    Last Post: 09-29-2015, 12:40 PM
  4. Replies: 2
    Last Post: 08-17-2015, 09:53 AM
  5. Calculate days between two dates by VBA
    By hhuuhn12 in forum Programming
    Replies: 16
    Last Post: 12-10-2013, 03:11 PM

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