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 > Help with Update statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-06-10, 12:28
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
Help with Update statement

Hi Guys,

This is my first thread here. I am trying to update a column with the sum of the column(Col1) but it should be grouped by on another column (Col2)

Code:
update Table set Col1 = (select sum(Col1)
from Table
where Col3=1
group by Col1,Col2)
where Col1=1
This is the sample data am trying with
Code:
COl1 Col2 Col3
0.4   2     1
0.8    2     1
5    3      1
The output should be
Code:
1.2          2         1
1.2          2          1
5              3          1
Thanks For your help in advance.
Regards,
Magesh
Reply With Quote
  #2 (permalink)  
Old 04-06-10, 12:50
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Is this what you want?

Code:
update Table as t set Col1 = (select sum(Col1)
from Table as x
where x.Col3=1 and x.col2 = t.col2
)
Andy
Reply With Quote
  #3 (permalink)  
Old 04-06-10, 13:30
mac4rfree mac4rfree is offline
Registered User
 
Join Date: Jul 2009
Posts: 52
Yes Andy.. that worked like charm.. thanks again..
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