tags:

views:

61

answers:

3
+6  Q: 

SQL Max Question

So I want to get the row with the most recent date, max(asofdate) but I am MySQL illiterate and can't seem to get it. It my head select * from Reports.InternalLoanExposureFlat where asofdate = max(asofdate) seems to make sense but the console seems to disagree with me.

Thanks in advance.

+3  A: 

Try:

SELECT * FROM Reports.InternalLoanExposureFlat
WHERE asofdate = (SELECT MAX(asofdate) FROM Reports.InternalLoanExposureFlat) 
gkrogers
this is the correct way of finding man(asofdate)!
Frank Computer
what if several rows contains the `asofdate` equals to `MAX(asofdate)`?
zerkms
Then you will get them all.
gkrogers
A: 

I agree with the console ;-).

The max function returns the maximum of a group or the complete table.

Try :

SELECT somecolumn, MAX(asofdate) FROM mytable GROUP BY somecolumn

SELECT MAX(asofdate) FROM mytable
Peter Tillemans
+9  A: 

If you don't want the risk of returning multiple results then you should use this:

SELECT * 
FROM Reports.InternalLoanExposureFlat
ORDER BY asofdate DESC
LIMIT 1
Mark Byers
what a costly method of finding out the max(asofdate)!
Frank Computer
@Frank Computer: He didn't say he wanted to get max(asofdate), he said to get the row with the most recent date.
Mark Byers
@Mark- as I read his question, he explicitly stated max(asofdate)
Frank Computer
He wants to get the row with the most recent date, and Mark's query will work, as will mine (I think). The question is, is one or the other better, e.g. in terms of readability, performance, portability, etc?
gkrogers