Reputation: 163
Here's the scenario. I have employees and they evaluated yearly for different categories. I need: the full row of the latest evaluation for each category.
Example:
Employee Table:
EmpID Name
454 John
Evals Table:
id EmpID Category EvalDate Note
1 454 A 2016-01-01 Excellent
2 454 A 2017-02-15 Not so good
3 454 B 2016-01-01 Poor
4 454 B 2017-02-01 Good
I need a query that returns the latest eval (the full row) for each category:
EmpID Category EvalDate Note
454 A 2017-02-15 Not so good
454 B 2017-02-01 Good
My query so far:
SELECT
evals.EmpID,
evals.Category,
evals.Note,
MAX(evals.EvalDate) as LatestEval
FROM
evals
WHERE
evals.EmpID = 454
GROUP BY
evals.Category
This unfortunately returns the latest date for each category but the note field from older evals.
Upvotes: 1
Views: 48
Reputation: 5040
You might give this a try:
SELECT
evals.EmpID,
evals.Category,
evals.Note,
evals.EvalDate
FROM Employee emp
JOIN Evals evals
ON emp.EmpID = evals.EmpID
AND eval.EvalDate =
(SELECT MAX(EvalDate)
FROM Evals a
WHERE a.Category = evals.category
AND a.EmpID = emp.EmpID GROUP BY e.EmpID,e.Category)
GROUP BY evals.EmpID,evals.Category
Upvotes: 2