I know there are a lot of questions regarding using max with a group by but I promise this one is trickier. (ok…so it was so easy it was answered in 10 minutes)
Here’s a sample table
|ID|name| end_time |
|01|Bob |2012-02-09 3:00:00|
|02|Bob |2012-02-09 3:15:00|
|03|Ted |2012-02-09 3:00:00|
|04|Ted |null |
What I want is to return the max time grouped by name but exclude names that have a null end_time.
I started with
SELECT * FROM table t,(
SELECT MAX(end_time), name FROM table
GROUP BY name
) last_entry
WHERE t.name = last_entry.name
AND t.end_time = last_entry.end_time
The problem is that this gives me ID 02 and 03 whereas I want it to return only 02.
If I understood your question correctly, you can solve it by excluding names with null end times in any other row: