get max values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkshetty
    New Member
    • Oct 2007
    • 6

    get max values

    co_1 col_2 co_3 result

    12.00 11.00 21.00
    11.00 23.00 43.00
    22.00 23.00 43.00
    22.00 43.00 23.00


    sql query to update RESULT with max of (co_1 , col_2 , col_3)
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by jkshetty
    co_1 col_2 co_3 result

    12.00 11.00 21.00
    11.00 23.00 43.00
    22.00 23.00 43.00
    22.00 43.00 23.00


    sql query to update RESULT with max of (co_1 , col_2 , col_3)
    Do you want to update RESULT Column with max(col_1,col_2 ,col_3) which is 43.00 for all the rows or you want to update the max value of each row?

    Comment

    • jkshetty
      New Member
      • Oct 2007
      • 6

      #3
      Originally posted by amitpatel66
      Do you want to update RESULT Column with max(col_1,col_2 ,col_3) which is 43.00 for all the rows or you want to update the max value of each row?
      yes i want to update the max value of each row?


      each rows high

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by jkshetty
        yes i want to update the max value of each row?


        each rows high
        Could you please POST if there are any other column in the table (primary key)?

        Comment

        • jkshetty
          New Member
          • Oct 2007
          • 6

          #5
          hi

          its somthing u have 4 or more values u have to choose the maximum value out of it.

          eg : to choose maximum value out of colum ( select max(xxx) from table group by xxx )

          the same way maximum of single row.

          Comment

          • bashaa
            New Member
            • Nov 2007
            • 2

            #6
            Originally posted by jkshetty
            hi

            its somthing u have 4 or more values u have to choose the maximum value out of it.

            eg : to choose maximum value out of colum ( select max(xxx) from table group by xxx )

            the same way maximum of single row.

            there is no stright way to find the max value in a row among the column.

            see the following example ,to find the solution for ur problem.
            replace the ur identity column with id column.


            create table test2(id int identity(1,1),m ar1 decimal(15,3),m ar2 decimal(15,3),m ar3 decimal(15,3),r es decimal(15,3))

            insert into test2 values(12.00, 11.00, 21.00,0)
            insert into test2 values(11.00, 23.00, 43.00, 0)
            insert into test2 values(22.00, 23.00, 43.00,0)
            insert into test2 values(22.00, 43.00, 23.00,0)

            update max value
            -----------------------
            UPDATE T1 SET T1.res=T2.MAXVA L FROM test2 T1,(SELECT CASE WHEN mar1>mar2 THEN CASE WHEN mar1>mar3 THEN mar1 ELSE mar3 END
            ELSE CASE WHEN mar2>mar3 THEN mar2 ELSE mar3 END
            END AS MAXVAL ,id FROM test2 )T2 WHERE T2.ID=T1.ID

            Comment

            • sayedul
              New Member
              • Oct 2007
              • 12

              #7
              Originally posted by jkshetty
              co_1 col_2 co_3 result

              12.00 11.00 21.00
              11.00 23.00 43.00
              22.00 23.00 43.00
              22.00 43.00 23.00


              sql query to update RESULT with max of (co_1 , col_2 , col_3)
              The following will work:

              Code:
              Update TableName set Result =
              case when (case when col_1 > col_2 then col_1 else col_2 end) > col_3 then 
              (case when col_1 > col_2 then col_1 else col_2 end) else col_3 end

              Comment

              Working...