Showing posts with label bobupdate. Show all posts
Showing posts with label bobupdate. Show all posts

Thursday, February 9, 2012

Advise on Multiple Column Updates

Using SQL2000, is there another way of optimizing the original query below.
TIA,
Bob

Update Transaction
set field1 = (select (sum(tax)-sum(credit))/sum(credit) from tblOrder where
tblOrder.id = Transaction.id),
field2 = (select avg(price) from tblOrder where tblOrder.id =
Transaction.id),
field3 = (select min(cost) from tblOrder where tblOrder.id = Transaction.id)
etc..
(there are six additional updates)

Is this a quicker way:

Update Transaction
SET field1 = (sum(tax)-sum(credit))/sum(credit) , field2 = avg(price) ,
field3 = min(cost) , etc....
FROM tblOrder
WHERE tblOrder.id = Transaction.idThe format is:
UPDATE <table> SET (<columnlist>) = (SELECT <select_list> FROM ...)
WHERE ...

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab