MAW74656
MAW74656

Reputation: 3549

Need help with odd Access query

I have an existing Access database which contains training records for employees. If an employee has been trained or has been scheduled for training there is a record in the linking table. If not, there is no record. I want to query all employees not trained on a certain thing, but there aren't any records.

Database is like follows:

Table: Trainings   
TrainingID    AutoNumber   
Description   Text   
...

Table: EmployeeTrainings
EmployeeTrainingID     AutoNumber   
TrainingID             Number
EmployeeID             Number
ScheduledDate          Date/Time
...

Table: Employees  
EmployeeID       Number  
FullName         Text  
...

I want result like following with following fields for employees who are not trained on a given training:

Trainings.Description, Employees.FullName, EMployeeTrainings.ScheduledDate

How can I achieve this? I need some guidance.

Upvotes: 2

Views: 77

Answers (1)

DRapp
DRapp

Reputation: 48179

I think this should get it for you... However, since they would not have been trained, we can't make up a Training Date for them, hence I've only included the two columns...

select
      t.Description,
      emp.FullName,
      et.ScheduledDate
   from
      trainings t
         join employees emp
            left join EmployeeTrainings et
               on emp.EmployeeID = et.EmployeeID
               AND et.TrainingID = t.TrainingID
   where
      t.TrainingID IN ( 1, 3 )
   order by
      t.Description,
      emp.FullName

This should get you what you want... by doing a LEFT JOIN, it returns all entries on the left side of the join with records to the right table. If no such records on the right side (hence the employees that have never been scheduled), their scheduled date value will come out as NULL... For those that DID find proper match, will have their dates included in the result set. Not positive about "Access" syntax on the IN clause... but it SHOULD work as it would in other SQL engines.

Upvotes: 2

Related Questions