Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sum query question

Author  Topic 

1821
Starting Member

18 Posts

Posted - 2008-04-14 : 14:38:46
Hey guys, sorry for a newbie question but here goes:

I have a table with col1, col2, col3, col4

where col1,col2 and col3 are values and col4 is the sum of the values.

The problem is that the sum column is currently empty and I was wondering how do I write a query that will add the sum to each row?

Thanks in advance for any help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-14 : 14:41:43
UPDATE YourTable
SET col4 = col1 + col2 + col3

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

1821
Starting Member

18 Posts

Posted - 2008-04-14 : 14:52:03
Many thanks for your reply!

Just for future reference how would you do this if there was not data already in col1, col2 and col3 and all the data was going to be added at the same time?

thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-14 : 14:55:38
Insert into desttable (col1,col2,col3,col4)
SELECT col1,col2,col3,col1+col2+col3
FROM Source
Go to Top of Page

1821
Starting Member

18 Posts

Posted - 2008-04-14 : 14:59:16
Thats great, thanks for your help.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-14 : 15:26:51
Or you could make the Col4 as calculated column?

ALTER TABLE DestTable
ALTER COLUMN Col4 (Col1 + Col2 + Col3)



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -