If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Query Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-11-09, 22:39
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Query Help

Can someone tell me is there better way to write following query :--
maintab table updated from secondtab table

Maintab
key cola qty qty1
k1 row1 10 2
k1 row2 20 3
k2 row1 100 2

secondtab
Sno key cola qty
1 k1 row1 2
1 k1 row2 3
2 k2 row1 6

update maintab ma set ma.qty = ma.qty + (select SI1.qty from secondtab SI1 where SI1.sno = '1' and SI1.cola = ma.cola and SI1.key = ma.key) ,
ma.qty1 = ma.qty1 - (select SI2.qty from secondtab SI2 where SI2.sno = '1' and SI2.cola = ma.cola and SI2.key = ma.key)
where (ma.key,ma.cola) in (select so.key, So.cola from secondtab So where So.sno = '1')

Expected Result
Maintab
key cola qty qty1
k1 row1 12 0 Record updated
k1 row2 23 0 Record updated
k2 row1 100 2 Not effected

My concern is to update the qty back to Maintab 2 columns, i am running subquery twice

Please advise, Thanks
Reply With Quote
  #2 (permalink)  
Old 02-12-09, 06:17
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Consider using the MERGE statement instead.
Reply With Quote
  #3 (permalink)  
Old 02-12-09, 16:51
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Thank you Nick
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On