Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to get max on each row [modified]

How to get max on each row [modified]

Scheduled Pinned Locked Moved Database
databasehelptutorialquestionannouncement
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xoxoxoxoxoxox
    wrote on last edited by
    #1

    Hi Guys, I want to compare three columns on each row and get the maximum value. I tried using GREATEST(col1,col2,...) function but it didn't work (I hve SQL version 8.0 installed). Is there any way to get max of several colunms per row? Here is how it should look like. =========================================================== ID, measurement_01, measurement_02, measurement_03, maximum =========================================================== 1, 300, 350, 325, 350 2, 225, 275, 400, 400 3, 100, 500, 300, 500 =========================================================== I'll appreciate any help Thanks -- modified at 16:06 Friday 26th January, 2007

    A 1 Reply Last reply
    0
    • X xoxoxoxoxoxox

      Hi Guys, I want to compare three columns on each row and get the maximum value. I tried using GREATEST(col1,col2,...) function but it didn't work (I hve SQL version 8.0 installed). Is there any way to get max of several colunms per row? Here is how it should look like. =========================================================== ID, measurement_01, measurement_02, measurement_03, maximum =========================================================== 1, 300, 350, 325, 350 2, 225, 275, 400, 400 3, 100, 500, 300, 500 =========================================================== I'll appreciate any help Thanks -- modified at 16:06 Friday 26th January, 2007

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      What database-engine and front-end tool are you using?

      X 1 Reply Last reply
      0
      • A andyharman

        What database-engine and front-end tool are you using?

        X Offline
        X Offline
        xoxoxoxoxoxox
        wrote on last edited by
        #3

        Its SQL server 2000. SQL Query Analayzer version 8.0

        A 1 Reply Last reply
        0
        • X xoxoxoxoxoxox

          Its SQL server 2000. SQL Query Analayzer version 8.0

          A Offline
          A Offline
          andyharman
          wrote on last edited by
          #4

          The easiest way (assuming you only need the maximum of 2 columns) would be:

          select id, measurement_01, measurement_02, measurement_03,
          case
          when measurement_01 >= measurement_02 and measurement_01 >= measurement_03
          then measurement_01
          when measurement_02 >= measurement_03
          then measurement_02
          else measurement_03
          end as maximum
          from #temp_ajh
          order by id

          Regards Andy

          M X 2 Replies Last reply
          0
          • A andyharman

            The easiest way (assuming you only need the maximum of 2 columns) would be:

            select id, measurement_01, measurement_02, measurement_03,
            case
            when measurement_01 >= measurement_02 and measurement_01 >= measurement_03
            then measurement_01
            when measurement_02 >= measurement_03
            then measurement_02
            else measurement_03
            end as maximum
            from #temp_ajh
            order by id

            Regards Andy

            M Offline
            M Offline
            Michael Potter
            wrote on last edited by
            #5

            Or you can flatten the table (as it should have been designed in the first place). SELECT m.ID, m.measurement_01, m.measurement_02, m.measurement_03, MAX (FlatTable.measurement_01) FROM MeasurementTable m INNER JOIN (SELECT ID, measurement_01 FROM MeasurementTable UNION SELECT ID, measurement_02 FROM MeasurementTable UNION SELECT ID, measurement_03 FROM MeasurementTable) AS FlatTable ON (m.ID = FlatTable.ID) GROUP BY m.ID, m.measurement_01, m.measurement_02, m.measurement_03

            1 Reply Last reply
            0
            • A andyharman

              The easiest way (assuming you only need the maximum of 2 columns) would be:

              select id, measurement_01, measurement_02, measurement_03,
              case
              when measurement_01 >= measurement_02 and measurement_01 >= measurement_03
              then measurement_01
              when measurement_02 >= measurement_03
              then measurement_02
              else measurement_03
              end as maximum
              from #temp_ajh
              order by id

              Regards Andy

              X Offline
              X Offline
              xoxoxoxoxoxox
              wrote on last edited by
              #6

              Both solutions are perfect! Thank you very much for your answer. -- modified at 17:21 Friday 26th January, 2007

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups