I'm attempting to pull some custom reports from a mandated SQL program than we must use at work and I'm running into a couple issues. I can pull all the data I need easily but for each unique person id/task id combination I only need the most current value. Additionally, if possible I want the latest value from either the due date or the waiver date column whichever is greater.
PersonnelTrainingEvent PersonnelID TrainingEventTypeID DueDate WaiverDate Personnel ID TrainingEventType ID Taskcode PersonnelDetail PersonnelID
5351 25947 1/1/1900 1/1/1900 5351 25947 Mob2 5351
5351 28195 8/1/2012 1/1/1900 5351 28195 CA01 5351
5351 26551 7/29/2010 1/1/1900 5351 26551 Mob10 5351
5351 25947 1/31/2012 1/1/1900 5351 25947 Mob2 5351
5351 28196 11/1/2012 1/1/1900 5351 28196 CA02 5351
5418 28195 1/1/1900 1/1/1900 5418 28195 CA01 5418
5418 30174 1/1/1900 1/1/1900 5418 30174 PJ18 5418
5418 28624 1/31/2014 2/1/2014 5418 28624 GA42 5418
5418 28595 6/30/2014 6/30/2014 5418 28595 GA43 5418
5418 28196 1/1/1900 1/1/1900 5418 28196 CA02 5418
6022 28195 3/3/2011 1/1/1900 6022 28195 CA01 6022
6022 28885 10/31/20121/1/1900 6022 28885 CA07 6022
6022 28884 1/1/1900 1/1/1900 6022 28884 CA06 6022
6022 28884 1/31/1901 1/1/1900 6022 28884 CA06 6022
6022 28196 1/1/1900 1/1/1900 6022 28196 CA02 6022
6022 28196 2/28/2011 1/1/1900 6022 28196 CA02 6022
6022 28624 9/30/2013 1/1/1900 6022 28624 GA42 6022
6022 28595 2/28/2014 1/1/1900 6022 28595 GA43 6022
6022 30174 2/28/2014 1/1/1900 6022 30174 PJ18 6022
Here is the query I'm using...
SELECT
PersonnelTrainingEvent.PersonnelID AS [PersonnelTrainingEvent PersonnelID]
,PersonnelTrainingEvent.TrainingEventTypeID
,PersonnelTrainingEvent.DueDate
,PersonnelTrainingEvent.WaiverDate
,Personnel.ID AS [Personnel ID]
,TrainingEventType.ID AS [TrainingEventType ID]
,TrainingEventType.Taskcode
,PersonnelDetail.PersonnelID AS [PersonnelDetail PersonnelID]
FROM
PersonnelTrainingEvent
INNER JOIN TrainingEventType
ON PersonnelTrainingEvent.TrainingEventTypeID = TrainingEventType.ID
INNER JOIN Personnel
ON PersonnelTrainingEvent.PersonnelID = Personnel.ID
INNER JOIN PersonnelDetail
ON Personnel.ID = PersonnelDetail.PersonnelID
WHERE
TrainingEventType.Taskcode IN (N'GA43', N'MOB2', N'CA01', N'CA02', N'Mob10', N'PJ67', N'CA06', N'CA07', N'T104', N'GA42', N'PJ18')
Group By
Personnel.ID, TrainingEventType.Taskcode;
I'm currently on vacation and getting glared at by my wife but I've been working on this query for 3 weeks now and I'm pounding my head against the wall. I've included a sample of the preferred outcome below...
PersonnelTrainingEvent PersonnelID TrainingEventTypeID DueDate WaiverDate Personnel ID TrainingEventType ID Taskcode PersonnelDetail PersonnelID
5351 28195 8/1/2012 1/1/1900 5351 28195 CA01 5351
5351 26551 7/29/2010 1/1/1900 5351 26551 Mob10 5351
5351 25947 1/31/2012 1/1/1900 5351 25947 Mob2 5351
5351 28196 11/1/2012 1/1/1900 5351 28196 CA02 5351
5418 28195 1/1/1900 1/1/1900 5418 28195 CA01 5418
5418 30174 1/1/1900 1/1/1900 5418 30174 PJ18 5418
5418 28624 1/31/2014 2/1/2014 5418 28624 GA42 5418
5418 28595 6/30/2014 6/30/2014 5418 28595 GA43 5418
5418 28196 1/1/1900 1/1/1900 5418 28196 CA02 5418
6022 28195 3/3/2011 1/1/1900 6022 28195 CA01 6022
6022 28885 10/31/20121/1/1900 6022 28885 CA07 6022
6022 28884 1/31/1901 1/1/1900 6022 28884 CA06 6022
6022 28196 2/28/2011 1/1/1900 6022 28196 CA02 6022
6022 28624 9/30/2013 1/1/1900 6022 28624 GA42 6022
6022 28595 2/28/2014 1/1/1900 6022 28595 GA43 6022
6022 30174 2/28/2014 1/1/1900 6022 30174 PJ18 6022
Here are the links to the other answers I've looked at but I'm a learn by doing kinda guy and these seemed to help a little but I'm not understanding all the syntax...
SQL Select, Specific Rows based on multiple conditions?
SQL server select distinct rows using most recent value only
SQL Select with Group By and Order By Date
SQL server select distinct rows using values before a certain date
How to select only the latest rows for each user?
Get Distinct rows from a result of JOIN in SQL Server
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47479
Selecting latest rows in subgroups
I appreciate any help as I'm working to learn to do this myself. I will provide any input requested or a screenshot if I increase my rep enough to allow that. Thanks!