Update column3?

  • write a query to update column3 from table such that column3 contains

    whatever be the greater value among column1 and column2 columns.

    e.g

    P_key Column1 Column2 Column3

    1 20 30 -

    2 20 25 -

    3 30 20 -

    So that, the column3 should contain 30, 25 30 resp.

  • You should be able to do this with a simple case statement...

    Something like the following...

    Update mytbl

    SET col3 = CASE WHEN col1 >= col2 THEN col1

    WHEN col1 < col2 then col2 END

    To help us help you read this[/url]For better help with performance problems please read this[/url]

  • Please read the guidelines on posting queries.

    You need to provide sample script to recreate the table and insert data in to it.

    Having said that, this should suffice in your case:

    update myTable

    set column3=

    case

    when column1>column2 then column1

    else column2

    end

    How To Post[/url]

  • Thanks!

  • Thanks

  • I can't help getting the feeling that was homework and the OP didn't try.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply