Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2016
    Posts
    6

    Answered: Update group of rows in table

    Hi,

    My first post here...

    I need help with one UPDATE statement.

    I have one table similar to this:

    ID T_ID | TT
    ---|-----------------
    1 | 1dfgr3 | 9
    2 | 1dfgr3 | 9
    3 | 1dfgr3 | 9
    4 | 1dfgr3 | 3
    5 | 456sf6 | 9
    6 | 456sf6 | 9
    7 | 456sf6 | 5
    8 | 456sf6 | 9


    Now, I have altered table and added new column called TTT and final state of table should be like this:

    ID T_ID | TT | TTT
    ---|---------------------------
    1 | 1dfgr3 | 9 | 3
    2 | 1dfgr3 | 9 | 3
    3 | 1dfgr3 | 9 | 3
    4 | 1dfgr3 | 3 | 3
    ---------------------------------
    5 | 456sf6 | 9 | 5
    6 | 456sf6 | 9 | 5
    7 | 456sf6 | 5 | 5
    8 | 456sf6 | 9 | 5

    So, UPDATE should be like 'UPDATE TABLE1 SET TTT=(select TT from TABLE1 where TT<>9) but for group of values displayed in T_ID column.

    So, question is, how can I update table so it will look for same values in T_ID column, and then search for value in TT which is <> 9 and then make and update to TTT column with single value?

    Regards,

  2. Best Answer
    Posted by mark.bb

    "Hi,

    UPDATE TABLE1 A SET TTT=(
    select min(TT)
    from TABLE1 B
    where B.TT<>9 and B.T_ID=A.T_ID
    )"


  3. #2
    Join Date
    Jul 2016
    Location
    Moscow
    Posts
    294
    Provided Answers: 45
    Hi,

    UPDATE TABLE1 A SET TTT=(
    select min(TT)
    from TABLE1 B
    where B.TT<>9 and B.T_ID=A.T_ID
    )
    Regards,
    Mark.

  4. #3
    Join Date
    Oct 2016
    Posts
    6
    Simple and efficient. Thank you

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •