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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Select query sum all 12 cell from 1 row jan to dec

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2009-08-28 : 16:53:19
I have a table, it has columns from jan to dec.

to begin with the table design itself is bad.

This table has all 12 rows starting from jan to dec, and amounts specified in each month.

Is it possible to sum all cell from jan to dec using the fundid, this field is a primary key field.

select (jan+feb+mar+....+Dec) from table_cshflw where fundid=@fundid

Thank you very much for the helpful info.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-28 : 17:04:36
Your code looks fine to me - did you try it? The only possible problem is if there are any NULL values in there. You would have to use a coalesce or isnull function for each column. coalesce(jan,0) + ...

The only other way would be to use UNPIVOT (in 2005 and later) to get it all in one column then you could sum the column.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -