Results 1 to 4 of 4
  1. #1
    AccessPower's Avatar
    AccessPower is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2016
    Posts
    165

    Select latest record for each Employee and Course

    Happy Holidays to everyone!

    So I have a sql query that pulls training dates for all employees and the courses they've completed. The query pulls a refresh rate of the course, the date the employee completed the course, and then calculates the expiration date based on that data.

    What I am trying to do is to only get the latest completed training date. For instance, John completed his CPR Training annually in 2014, 2015, and 2016. I only want to see the record from 2016.



    Here is the query so far:

    Code:
    SELECT tblEmployees.FullName, tblCourses.Course, tblCourses.RefreshRate, tblEmployeeTrainingTracker.LastCompleted, Switch(RefreshRate='Annual', DateAdd("yyyy",1,LastCompleted), RefreshRate='2 Year', DateAdd("yyyy",2,LastCompleted), RefreshRate='3 Year', DateAdd("yyyy",3,LastCompleted), RefreshRate='5 Year', DateAdd("yyyy",5,LastCompleted)) AS CourseExpires
    
    FROM (tblCourses INNER JOIN tblEmployeeTrainingTracker ON tblCourses.[ShipsID] = tblEmployeeTrainingTracker.[ShipsID]) 
    INNER JOIN tblEmployees ON tblEmployeeTrainingTracker.[EmployeeID] = tblEmployees.[EmployeeID] WHERE tblCourses.RefreshRate <> 'None' 
    
    ORDER BY tblEmployees.FullName, tblCourses.Course;
    I tried using the max function on tblEmployeeTrainingTracker.LastCompleted, but it just errors out. I am going to keep trying, maybe with some nested stuff, but I figured I'd post on here to colab and get any advice from the gurus.

    Thanks for everything you guys do.
    Last edited by AccessPower; 12-16-2016 at 01:16 PM.

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I will tell you how you can do it in a series of two queries. If you like, you can then imbed the one into the other afterwards, if you prefer one nested query.

    First, create a query that has FullName, Course, and LastCompleted date.
    Then, click the Totals button to make it an Aggregate Query, and switch the Totals row value of "Group By" under the LastCompleted date to "Max" (leave the other two "Group By" values alone).
    This will now give you the last completed date for each employee/course combination.

    If you want to see more details (the other fields), create a new query that links this query back to your original tables, being sure to include the LastCompleted date in your join, and return any fields that you want.

  3. #3
    AccessPower's Avatar
    AccessPower is offline Competent Performer
    Windows 10 Access 2013 64bit
    Join Date
    Oct 2016
    Posts
    165
    Joe,

    Thank you very much for your help. This was really a unique challenge for me. I did some slight modification to your advice. I ended up having to include the RefreshRate within the first query. Other than that it works perfectly. Here are the two queries that I used:

    Code:
    SELECT tblEmployees.Name, tblCourses.Course, tblCourses.RefreshRate, Max(tblEmployeeTrainingTracker.LastCompleted) AS MaxOfLastCompletedFROM (tblCourses INNER JOIN tblEmployeeTrainingTracker ON tblCourses.[ShipsID] = tblEmployeeTrainingTracker.[ShipsID]) INNER JOIN tblEmployees ON tblEmployeeTrainingTracker.[EmployeeID] = tblEmployees.[EmployeeID]
    GROUP BY tblEmployees.Name, tblCourses.Course, tblCourses.RefreshRate
    HAVING (((tblCourses.RefreshRate)<>'None'))
    ORDER BY tblEmployees.Name, tblCourses.Course, tblCourses.RefreshRate, Max(tblEmployeeTrainingTracker.LastCompleted);

    Code:
    SELECT qryLatestTraining.Name, qryLatestTraining.Course, qryLatestTraining.MaxOfLastCompleted, Switch(qryLatestTraining.RefreshRate='Annual',DateAdd("yyyy",1,MaxOfLastCompleted),qryLatestTraining.RefreshRate='2 Year',DateAdd("yyyy",2,MaxOfLastCompleted),qryLatestTraining.RefreshRate='3 Year',DateAdd("yyyy",3,MaxOfLastCompleted),qryLatestTraining.RefreshRate='5 Year',DateAdd("yyyy",5,MaxOfLastCompleted)) AS CourseExpires
    FROM qryLatestTraining;
    This worked out perfectly. No errors.

    Thank you for the mentoring once again.

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You are welcome!

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

Similar Threads

  1. Trying to select the latest date by month
    By Villa123 in forum Queries
    Replies: 3
    Last Post: 08-01-2016, 03:56 PM
  2. Query: select latest date within given category
    By martinhough in forum Queries
    Replies: 4
    Last Post: 10-22-2013, 12:34 PM
  3. Using Max Function to display latest record
    By jamarogers in forum Forms
    Replies: 4
    Last Post: 09-29-2013, 03:13 PM
  4. Get latest record
    By Sweet16 in forum Queries
    Replies: 9
    Last Post: 09-23-2011, 01:39 PM
  5. Query latest record
    By asherbear in forum Queries
    Replies: 5
    Last Post: 08-02-2010, 03:58 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