I am HAVING trouble with my GROUP BY query (the trickiest of them all at least for me).
What I want to do is retrieve the most recent modified records grouped by another column. The problem as I see it is that grouping returns the only first record it finds (in the group) disregarding the ORDER BY (except when returning the final result).
Here is a simplified version of my code.
SELECT events.id, events.name, events.type, events.modified_time
FROM events
GROUP BY events.type
ORDER BY event.modified_time DESC
Which will return:
1 | Event One | Birthday | Jan 1, 2012
2 | Event Two | Graduation | Jan 1, 2012
When in fact there is a 3rd record with a modified time that is later:
3 | Event Three | Birthday | Jan 2, 2012
I've tried using a HAVING clause of MAX(modified_time) but it doesn't return the correct results either. Maybe I just need to discuss this more with someone who is knowledgeable but if the question makes enough sense and you can tell what I need then maybe there is an easy answer.
BONUS QUESTION:
Is it possible to do this without a sub query?