I am pulling data from 3 separate employee related tables, all joined on SSN. Because some employees have transferred within the company multiple times they have multiple hire/separation dates in the system, so when I pull from that table with the hire dates, it duplicates the row by the number of hire/separation dates in the system. Here's the data sample as it's pulled:
SSN Name Pay_Date Hire_Date
123456789 John Doe 5/1/2012 1/1/2001
123456789 John Doe 5/1/2012 2/5/2005
123456789 John Doe 5/1/2012 3/1/2012
123456789 John Doe 5/15/2012 1/1/2001
123456789 John Doe 5/15/2012 2/5/2005
123456789 John Doe 5/15/2012 3/1/2012
123456789 John Doe 5/29/2012 1/1/2001
123456789 John Doe 5/29/2012 2/5/2005
123456789 John Doe 5/29/2012 3/1/2012
The query:
SELECT
SSN, Name, Pay_Date, Hire_Date
FROM Personnel as PER
LEFT JOIN Payroll as PAY on PER.SSN = PAY.SSN
LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN
ORDER BY Pay_Date(DESC)
To eliminate the rows with hire dates 1/1/2001 and 2/5/2005, I tried using the MAX function as follows with no luck. I tried using a variety of the examples posted on previous topics related to MAX but nothing is working.
SELECT
SSN, Name, Pay_Date, MAX(Hire_Date)
FROM Personnel as PER
LEFT JOIN Payroll as PAY on PER.SSN = PAY.SSN
LEFT JOIN HumanResources as HR on PER.SSN = HR.SSN
GROUP BY SSN
ORDER BY Pay_Date(DESC)
Again, I just need the row with the latest hire date. So the outcome should look like
SSN Name Pay_Date Hire_Date
123456789 John Doe 5/1/2012 3/1/2012
123456789 John Doe 5/15/2012 3/1/2012
123456789 John Doe 5/29/2012 3/1/2012