Results 1 to 11 of 11
  1. #1
    lbrannon is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6

    select the 2 earliest date records for each id

    I need a query to select the 2 earliest date records for each id in the following table and then to find the id's where the difference between those dates is greater than 5 days. I think I've got the DateDiff part down I just can't seem narrow down my dates to the first 2 for each id. Any ideas?



    WorkRequestID ComponentClass InspectionDate InspectionHistoryID NetReading PassFail
    15567 VALVE 1/5/2015 1446829 49485 FALSE
    15567 VALVE 1/9/2015 1451761 4 TRUE
    15568 VALVE 1/5/2015 1446890 6903 FALSE
    15568 VALVE 1/9/2015 1451762 1871 FALSE
    15568 VALVE 1/15/2015 1455215 4 TRUE
    15569 VALVE 1/5/2015 1446916 2540 FALSE
    15569 VALVE 1/9/2015 1451763 10 TRUE
    15570 VALVE 1/6/2015 1447819 534 FALSE
    15570 VALVE 1/8/2015 1450796 182 TRUE
    15571 VALVE 1/6/2015 1448730 515 FALSE
    15571 VALVE 1/9/2015 1451627 597 FALSE
    15571 VALVE 1/21/2015 1459640 2286 FALSE
    15571 VALVE 1/21/2015 1461034 2282 FALSE
    15571 VALVE 2/5/2015 1472222 0 TRUE
    15571 VALVE 3/17/2015 1499486 0 TRUE

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    lbrannon is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    So, I'm having a problem with it selecting the 2 newest dates instead of the oldest. It doesn't matter what order I have the table in. Is there a way to get it to select the 2 oldest dates?

    Code:
    SELECT tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate
    FROM tblWorkReqInsp
    WHERE (((tblWorkReqInsp.InspectionDate) In (SELECT TOP 2 InspectionDate                            
       FROM tblWorkReqInsp AS Dupe                              
       WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID        
       ORDER BY Dupe.InspectionDate DESC, Dupe.WorkRequestID DESC)))
    ORDER BY tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate DESC;

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    The selection should be on a unique ID field. Dates are not unique. An autonumber field can serve.

    SELECT tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate
    FROM tblWorkReqInsp
    WHERE ID In (SELECT TOP 2 ID
    FROM tblWorkReqInsp AS Dupe
    WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID
    ORDER BY Dupe.InspectionDate DESC, Dupe.WorkRequestID DESC)
    ORDER BY tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate DESC;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    lbrannon is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    This is still giving me the same problem. I tried sorting and saving the table several different ways before adding the autonumber ID field, but it doesn't seem to matter. It always autonumbers it the same way... with the oldest date first. And then the query selects the 2 newest dates. Am I going to have to manually enter the ID field so that the SELECT TOP will give me the 2 oldest dates?

  6. #6
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,451
    Order dates by DESC will give you the newest first. You also don't need to order the subquery by workrequestID since only the one workrequestid is 'in play' from your criteria. Try

    Code:
    SELECT tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate
     FROM tblWorkReqInsp
     WHERE InspectionHistoryID In (SELECT TOP 2 InspectionHistoryID                            
       FROM tblWorkReqInsp AS Dupe                              
       WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID        
       ORDER BY Dupe.InspectionDate)
     ORDER BY tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate DESC;

  7. #7
    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,743
    I copied your data and imported it into Access, and added an autonumber ID to get the following.

    Note: I am in Canada and my regional dates are DD/MM/YYYY format.

    ID WorkRequestID ComponentClass InspectionDate InspectionHistoryID NetReading PassFail
    1 15567 VALVE 01/05/2015 1446829 49485 FALSE
    2 15567 VALVE 01/09/2015 1451761 4 TRUE
    3 15568 VALVE 01/05/2015 1446890 6903 FALSE
    4 15568 VALVE 01/09/2015 1451762 1871 FALSE
    5 15568 VALVE 15/01/2015 1455215 4 TRUE
    6 15569 VALVE 01/05/2015 1446916 2540 FALSE
    7 15569 VALVE 01/09/2015 1451763 10 TRUE
    8 15570 VALVE 01/06/2015 1447819 534 FALSE
    9 15570 VALVE 01/08/2015 1450796 182 TRUE
    10 15571 VALVE 01/06/2015 1448730 515 FALSE
    11 15571 VALVE 01/09/2015 1451627 597 FALSE
    12 15571 VALVE 21/01/2015 1459640 2286 FALSE
    13 15571 VALVE 21/01/2015 1461034 2282 FALSE
    14 15571 VALVE 02/05/2015 1472222 0 TRUE
    15 15571 VALVE 17/03/2015 1499486 0 TRUE


    2 Newest records per group
    Code:
    SELECT tblWorkReqInsp.WorkRequestID
        ,tblWorkReqInsp.InspectionDate
    FROM tblWorkReqInsp
    WHERE ID IN (
            SELECT TOP 2 ID
            FROM tblWorkReqInsp AS Dupe
            WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID
            ORDER BY Dupe.WorkRequestID DESC
                ,Dupe.InspectionDate
            )
    ORDER BY tblWorkReqInsp.WorkRequestID
        ,tblWorkReqInsp.InspectionDate;
    Result:
    WorkRequestID InspectionDate
    15567 01/09/2015
    15567 01/05/2015
    15568 01/09/2015
    15568 01/05/2015
    15569 01/09/2015
    15569 01/05/2015
    15570 01/08/2015
    15570 01/06/2015
    15571 01/09/2015
    15571 01/06/2015

    2 Oldest records per group

    Code:
    SELECT tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate
    FROM tblWorkReqInsp
    WHERE (((tblWorkReqInsp.[ID]) In
            (SELECT TOP 2 ID
                      FROM tblWorkReqInsp AS Dupe
                      WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID
                      ORDER BY Dupe.WorkRequestID DESC
                                             ,Dupe.InspectionDate
                 )
    ))
    ORDER BY tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate;
    WorkRequestID InspectionDate
    15567 01/05/2015
    15567 01/09/2015
    15568 15/01/2015
    15568 01/05/2015
    15569 01/05/2015
    15569 01/09/2015
    15570 01/06/2015
    15570 01/08/2015
    15571 21/01/2015
    15571 21/01/2015

  8. #8
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,451
    This code should do the whole thing - technically the bit in red is not required because it would return false for datediff>5, but included for clarity
    Code:
    SELECT tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate, Oldest.InspectionDate, DateDiff("d",Oldest.InspectionDate,tblWorkReqInsp.InspectionDate) AS [Diff>5]
     FROM tblWorkReqInsp 
      INNER JOIN 
        (SELECT WorkRequestID, InspectionDate, InspectionHistoryID FROM tblWorkReqInsp
            WHERE InspectionHistoryID = (SELECT min(InspectionHistoryID)                            
                                                           FROM tblWorkReqInsp AS Dupe                              
                                                             WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID) AS Oldest
         ON tblWorkReqInsp.WorkRequestID=Oldest.WorkRequestID AND tblWorkReqInsp.InspectionHistoryID<>Oldest.InspectionHistoryID
    WHERE tblWorkReqInsp.InspectionHistoryID In (SELECT TOP 2 InspectionHistoryID                            
                                                                            FROM tblWorkReqInsp AS Dupe                              
                                                                             WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID        
                                                                               ORDER BY Dupe.InspectionDate)
         AND DateDiff("d",Oldest.InspectionDate,tblWorkReqInsp.InspectionDate)>5
     ORDER BY tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate DESC;

  9. #9
    lbrannon is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    This worked for selecting the correct dates! Thank you!

    Quote Originally Posted by Ajax View Post
    Order dates by DESC will give you the newest first. You also don't need to order the subquery by workrequestID since only the one workrequestid is 'in play' from your criteria. Try

    Code:
    SELECT tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate
     FROM tblWorkReqInsp
     WHERE InspectionHistoryID In (SELECT TOP 2 InspectionHistoryID                            
       FROM tblWorkReqInsp AS Dupe                              
       WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID        
       ORDER BY Dupe.InspectionDate)
     ORDER BY tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate DESC;

  10. #10
    lbrannon is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2015
    Posts
    6
    I couldn't get this to work. Keeps giving me a syntax error in FROM clause

    Quote Originally Posted by Ajax View Post
    This code should do the whole thing - technically the bit in red is not required because it would return false for datediff>5, but included for clarity
    Code:
    SELECT tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate, Oldest.InspectionDate, DateDiff("d",Oldest.InspectionDate,tblWorkReqInsp.InspectionDate) AS [Diff>5]
     FROM tblWorkReqInsp 
      INNER JOIN 
        (SELECT WorkRequestID, InspectionDate, InspectionHistoryID FROM tblWorkReqInsp
            WHERE InspectionHistoryID = (SELECT min(InspectionHistoryID)                            
                                                           FROM tblWorkReqInsp AS Dupe                              
                                                             WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID) AS Oldest
         ON tblWorkReqInsp.WorkRequestID=Oldest.WorkRequestID AND tblWorkReqInsp.InspectionHistoryID<>Oldest.InspectionHistoryID
    WHERE tblWorkReqInsp.InspectionHistoryID In (SELECT TOP 2 InspectionHistoryID                            
                                                                            FROM tblWorkReqInsp AS Dupe                              
                                                                             WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID        
                                                                               ORDER BY Dupe.InspectionDate)
         AND DateDiff("d",Oldest.InspectionDate,tblWorkReqInsp.InspectionDate)>5
     ORDER BY tblWorkReqInsp.WorkRequestID, tblWorkReqInsp.InspectionDate DESC;

  11. #11
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,451
    well I did type it freehand so may have missed a bracket or something.

    I've been through it again and looks like I have missed a bracket here

    ...
    WHERE Dupe.WorkRequestID = tblWorkReqInsp.WorkRequestID)) AS Oldest
    ...
    ...

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

Similar Threads

  1. Replies: 1
    Last Post: 05-09-2013, 09:13 AM
  2. Replies: 3
    Last Post: 04-19-2013, 12:49 PM
  3. Default earliest date and latest
    By Compufreak in forum Access
    Replies: 3
    Last Post: 01-10-2013, 07:17 AM
  4. Replies: 1
    Last Post: 02-17-2012, 04:43 PM
  5. Selecting Earliest and latest date
    By kstyles in forum Queries
    Replies: 10
    Last Post: 12-31-2010, 03:04 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