| Author |
Topic  |
|
|
Eithne
Starting Member
8 Posts |
Posted - 03/30/2005 : 05:45:28
|
I have four colums in a table and i need the sum of the four colums.
Can i do this in one sql statement as the sum function only works for the sum of the individual columns. |
|
|
LarsG
Constraint Violating Yak Guru
Sweden
284 Posts |
Posted - 03/30/2005 : 06:07:00
|
sum(coalesce(c1,0)+coalesce(c2,0)+coalesce(c3,0)+coalesce(c4,0))
|
 |
|
|
ijprasad
Starting Member
India
29 Posts |
Posted - 03/30/2005 : 06:16:23
|
you can add calculated column to calculate sum of 4 columns automatically by sql server
Inderjeet |
 |
|
|
Eithne
Starting Member
8 Posts |
Posted - 03/30/2005 : 06:17:18
|
is this correct:
select sum(POval1),sum(POval2),sum(POval3),sum(POval4) from projects where project_code = '123456' |
 |
|
|
AndrewMurphy
Flowing Fount of Yak Knowledge
Ireland
2915 Posts |
Posted - 03/30/2005 : 06:59:36
|
| Syntax is right....try it to see if the result is what you want. |
 |
|
|
Eithne
Starting Member
8 Posts |
Posted - 03/30/2005 : 07:24:38
|
the above statement only gives individual column totals, i need the total of all columns together, is the below the correct version of the first reply suggestion?
select sum(POval1(c41,0)+ POval2(c42,0) + POval3(c43+0) + POval4(c44,0)) from projects where project_code = 'ECP20362' |
 |
|
|
mr_mist
Grunnio
United Kingdom
1870 Posts |
Posted - 03/30/2005 : 07:35:55
|
No.
Leave the coalesce function where it is in the statement and substitute your four column names for the c1,c2,c3 and c4.
------- Moo. :) |
 |
|
|
Eithne
Starting Member
8 Posts |
Posted - 03/30/2005 : 07:44:30
|
| Great it works, thanks everyone for your help! |
 |
|
|
Bustaz Kool
Flowing Fount of Yak Knowledge
USA
1429 Posts |
Posted - 03/30/2005 : 20:26:35
|
If POVal returns a Null value, your results may not be what you want...
FYI
HTH
================================================================= In order to improve the mind, we ought less to learn than to contemplate. -Rene Descartes, philosopher and mathematician (1596-1650)
|
 |
|
| |
Topic  |
|