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 2008 Forums
 Transact-SQL (2008)
 Summarizing Column Into Total row

Author  Topic 

ranalk
Starting Member

49 Posts

Posted - 2012-08-05 : 10:46:09
Hi,

I have a table with 10 columns (int, subject) and 50 rows (clients).
I would like to add a row in the end that will summarize the amount of each column.


E.g.

Customer | Cheese | Milk | Bread
Mike | 5 | 7 | 10
Sam | 1 | 2 | 0

Total | 6 | 9 | 10


Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-05 : 14:49:25
use with cube or with rollup

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-05 : 14:50:42
if you're applying pivot you can use this

http://visakhm.blogspot.com/2012/04/display-total-rows-with-pivotting-in-t.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ranalk
Starting Member

49 Posts

Posted - 2012-08-06 : 03:12:45
Hi,

I saw you pivoting solution but in my example there is no need to use pivoting.
Cube and RollUp are summarizing each row and column.
I need to have just the total of each column and marked in the example above, without referring to the customer name.

can you assist?

thanks!
Go to Top of Page

ranalk
Starting Member

49 Posts

Posted - 2012-08-06 : 03:53:25
Ok , I managed by adding

union all select 'total',Sum(cheese),Sum(milk),Sum(Bread) from table
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-06 : 09:47:32
cool...the principle is same even if you dont require pivotting

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -