I have a report that I need to include data from another table. This table lists training dates for an employee. There are 4 types of training (1-4). The query for the report has several aggregate functions and that works properly but I don't think I can join the other criteria to the report query. In the report, I need to include the last two training dates for TrainType 1 and the most current date for all the other TrainTypes. The query for the report is quite complex so I think the best way to get the data I need is to create a new query from the other table and then in the text boxes in the report, reference the query for the training and training type. To do so, I need a query that will give me the 2 most recent training dates for each employee for each type. I am close but not quite there: Here is the Table Data:
I used the following query:
And this is close but not quite. It only lists the top 2 training dates of a certain type for all employees, not each employee. So in this case, it does not list employee 2540 for any training.
Or am I going about this all wrong?
Code:
tblTraining
EMP_ID TrainType TrainDate
2541 1 2/12/2012
1038 4 1/5/2012
2540 2 1/1/2012
2541 2 2/23/2012
1068 2 3/12/2012
2541 1 4/18/2012
2541 1 1/23/2012
2541 1 6/12/2012
Code:
select * from tblDocsIssued as T2
where docDate in (
SELECT TOP 2 docDate
FROM tblDocsIssued
where tblDocsIssued.docType = T2.docType
ORDER BY docDate DESC)
Or am I going about this all wrong?