tags:

views:

18

answers:

1

Hi all We are using SQL 2005 (MS),

I have a table that stores transactions. each time a specific event occurs 3 rows are added to for a specific transaction to that table.

Now we want to change (update) a column in row 1 called commision which in row 1 is null with the value of the same column from row 2 which has a value say 123. is that possible?

+1  A: 
update mytable a
   set commission = ( select commission
                        from mytable b
                       where a.transaction = b.transaction
                         and b.event = 'row 2 event' ) // some value that selects row 2
  where a.event = 'row 1 event'                        // some value that selects row 1
    and a.transaction = 'specific transaction'         // leave off for all transactions

From your comment I've reformatted the statement to be:

UPDATE PAYMENT a
SET COMMISSION= ( SELECT COMMISSION
                    FROM PAYMENT b
                   WHERE b.PAYMENT_SEQ = 3
                     AND a.transaction_ID = b.TRANSACTION_ID )
WHERE a.PAYMENT_SEQ = 1
  AND a.TRANSACTION_ID = 1234

Why use IN(1234) when =1234 is simpler and gets the same result. You can just match a.transaction_id = b.transaction_id in the inner select so you don't have to repeat your 1234 transaction selection on the inner select.

Paul Morgan
@Paul Morgan paul, from your example code the commision needs to be from the same table.. so what i am trying to do is set the commision of event 1 to the commision of event 2 from the exact same table... not a different one as u specified (mytable b).
andreas
I have tried this query :UPDATE PAYMENT SET COMMISSION= ( SELECT COMMISSION FROM PAYMENT P WHERE P.PAYMENT_SEQ = 3 AND TRANSACTION_ID IN (SELECT ID FROM TRANSACTION t WHERE ID IN (1234)) ) WHERE PAYMENT_SEQ = 1 AND TRANSACTION_ID IN (SELECT ID FROM TRANSACTION t WHERE ID IN (1234))
andreas
Umm...mytable a and mytable b refer to the same table.
Paul Morgan