This question already has an answer here:
I'm trying to get the last datetime record from a table that happens to store multiple status. My table looks like so:
+---------+------------------------+-------+
|filename |Dates |Status |
+---------+------------------------+-------+
|abc.txt |2012-02-14 12:04:45.397 |Open |
|abc.txt |2012-02-14 12:14:20.997 |Closed |
|abc.txt |2013-02-14 12:20:59.407 |Open |
|dfg.txt |2012-02-14 12:14:20.997 |Closed |
|dfg.txt |2013-02-14 12:20:59.407 |Open |
+---------+------------------------+-------+
The results should be
+---------+------------------------+-------+
|filename |Dates |Status |
+---------+------------------------+-------+
|abc.txt |2013-02-14 12:20:59.407 |Open |
|dfg.txt |2013-02-14 12:20:59.407 |Open |
+---------+------------------------+-------+
this working
Considering that max(dates) can be different for each filename, my solution :
http://sqlfiddle.com/#!3/c94a2/2
HTH
Exact syntax will of course depend upon database, but something like:
This will give you results exactly what you are asking for and displaying above. Fiddle: http://www.sqlfiddle.com/#!2/3af8a/1/0
Will return one result with the latest date.
Will return all results that have the same maximum date, to the milissecond.
This is for SQL Server. I'll leave it up to you to use the DATEPART function if you want to use dates but not times.