Question

I have a query that brings back a list of anyone that has any of the courses. I need a list of who has ALL the courses and not expired. I dont want to see the list of courses just a list of people who have all 5 courses. I am using MS Access 2010 SQL.

SELECT tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade,
       tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName,
       IIf(IsNull(CourseLength),"",DateAdd("m",[CourseLength],[Date])) AS ExpiryDate
FROM tblEmployees LEFT JOIN (tblCourses RIGHT JOIN tblTrainingHistory ON
      tblCourses.CourseID = tblTrainingHistory.CourseID) 
       ON tblEmployees.EmpID = tblTrainingHistory.EmpID
WHERE 
    (((tblCourses.CourseName) 
      In ("Confined Space","Manwatch","Ventis MX4","First Aid","CPR")) 
   AND ((IIf(IsNull([CourseLength]),Now(),DateAdd("m",[CourseLength],[Date])))>Now())
   AND ((tblEmployees.Active)=True))
GROUP BY tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade, 
      tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName,
      tblEmployees.EmpID, tblCourses.CourseLength    ;
Was it helpful?

Solution

Using your result table, group by all employee characteristics and select only those who have count(*) equal to 5 (as you have 5 courses).

SELECT LName, FName, Trade, Title, count(*) as courses
FROM(
SELECT tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade,
       tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName,
       IIf(IsNull(CourseLength),"",DateAdd("m",[CourseLength],[Date])) AS ExpiryDate
FROM tblEmployees LEFT JOIN (tblCourses RIGHT JOIN tblTrainingHistory ON
      tblCourses.CourseID = tblTrainingHistory.CourseID) 
       ON tblEmployees.EmpID = tblTrainingHistory.EmpID
WHERE 
    (((tblCourses.CourseName) 
      In ("Confined Space","Manwatch","Ventis MX4","First Aid","CPR")) 
   AND ((IIf(IsNull([CourseLength]),Now(),DateAdd("m",[CourseLength],[Date])))>Now())
   AND ((tblEmployees.Active)=True))
GROUP BY tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade, 
      tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName,
      tblEmployees.EmpID, tblCourses.CourseLength ) results
GROUP BY LName, FName, Trade, Title
HAVING count(*) = 5  ;

The subquery is probably not necessary, but as your query is quite complicated, I didn't want to mess with it. You can try and do this in one query only

See related question with simpler tables

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top