0

Lets say we have a DATES table with 3 columns: colYear(YEAR), colMonth(MONTH), colDay(DAY).

Rows are inserted like:

colYear colMonth colDay
   1       2        3   
   2       3        0   
   4       5        5   
   4       8        0    
   1      10       28

I need to find maximum date based on 3 columns. So that in this example right answer will be: 4 years, 8 months and 0 days is maximum (we check years, max are 4s, after that we check months max is 8, so we've found our result).

Is it possible? How to achieve this with sql?

1 Answer 1

5

use order by -

    SELECT * from DATES 
    ORDER BY colYear DESC, colMonth DESC, colDay DESC

Based on your DB you can limit the row count to 1.

so for oracle use rownum = 1 whereas for mysql it will be LIMIT 1

Sign up to request clarification or add additional context in comments.

3 Comments

Works just as i wanted, thank you for nice and simple solution.
i am glad it helped... also make note of what Sparky mentioned above if you need to limit your count to 1.
Sure ;) used top(1) before hes told. Thanks again.

Your Answer

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.