1

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

asked Feb 21, 2012 at 6:34

3 Answers 3

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.

answered Feb 21, 2012 at 6:45
Sign up to request clarification or add additional context in comments.

1 Comment

Yup this is what seems to work! and greatest also takes more values than date_a and b (there are 7 sometimes 8 dates)
2
select greatest(date_a,date_b) as recent from table
answered Feb 21, 2012 at 6:41

2 Comments

Max(greatest(date_a,date_b)) seems to work well (greatest results in many records)
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. +1
0

try this

SELECT IF(MAX(DATE_A)>MAX(DATE_B),MAX(DATE_A), MAX(DATE_B)) FROM table
answered Feb 21, 2012 at 6:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.