Return largest value from multiple columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akselo
    New Member
    • Sep 2007
    • 21

    Return largest value from multiple columns

    Does anyone know a good way to compare several columns and return the largest value, somewhat like what MAX does for a single column?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by akselo
    Does anyone know a good way to compare several columns and return the largest value, somewhat like what MAX does for a single column?
    Depends on how many columns you are comparing against. You could try the IF function. Have a look at this

    [CODE=mysql]select IF(max(co1a) > max(colb), max(cola), max(colb))[/CODE]

    Comment

    • akselo
      New Member
      • Sep 2007
      • 21

      #3
      Originally posted by r035198x
      Depends on how many columns you are comparing against. You could try the IF function. Have a look at this

      [CODE=mysql]select IF(max(co1a) > max(colb), max(cola), max(colb))[/CODE]
      Thank you for the quick response. I have six fields with different square footage information for land use types (residential, commercial, office, etc), and hope to return the largest value for the parcel in question, and while I considered the if statement, this method seems cumbersome as I would need to test each field against all others and end up with 6 different nested if statements. Could one define an array of six columns and return the largest?

      Comment

      • Jason Hallums
        New Member
        • Oct 2007
        • 2

        #4
        you can use the following:

        select greatest(100,67 ,876,76,234,100 98) from dual;

        can be used in any sql statement , just list the relevant comments inside the greatest () function

        Comment

        Working...