have a couple of dates in a few fields, basically
SELECT MAX(MAX(DATE_A),MAX(DATE_B)) from table
DATE_A and DATE_B are dates, i basically want the maximum of the latest date of either date A or date B
I searched online but max as a keyword makes it very hard to find what im looking for
3 Answers 3
SELECT MAX(GREATEST(date_a,date_b)) FROM table
This first uses GREATEST(date_a,date_b) to reduce these two columns into just one, containing the greatest value for each row.
The MAX then selects the one maximum value over all of these.
It also prevents repeated evaluation of MAX to make things a littler more efficient.
GREATEST documentation, MAX documentation.
Note that GREATEST operates across columns, whereas MAX operates across rows, which is why you need to use them both to get the single maximum value across both rows and columns.
1 Comment
select greatest(date_a,date_b) as recent from table
2 Comments
SELECT MAX(GREATEST(date_a,date_b)) from table is probably closer to what the OP had in mind but the GREATEST function is what he needs. +1try this
SELECT IF(MAX(DATE_A)>MAX(DATE_B),MAX(DATE_A), MAX(DATE_B)) FROM table