Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Max Question

Tags:

sql

mysql

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.

like image 425
Conner Avatar asked Jun 18 '10 20:06

Conner


People also ask

How do you find the max of something in SQL?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

What does Max () mean in SQL?

SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.

Can I use Max in having SQL?

MAX() function with HavingThe SQL IN OPERATOR which checks a value within a set of values and retrieve the rows from the table can also be used with MAX function.

How do I select the maximum two values in SQL?

Another solution that is described below with code examples can be used to solve the same issue Sql Max Of Two Values. SELECT MAX (column_name) FROM table_name WHERE column_name NOT IN (SELECT Max (column_name) FROM table_name);


1 Answers

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
like image 79
Mark Byers Avatar answered Oct 15 '22 05:10

Mark Byers