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.
    
        | 
                
                    | 
                            
                                | Author | Topic |  
                                    | caliguy1979Starting Member
 
 
                                        2 Posts | 
                                            
                                            |  Posted - 2009-09-14 : 20:18:03 
 |  
                                            | Hi,I don't know how exactly how to do this.  So for example I have the following set of data:Table: tbl_country_costsCountry		Type		CostNorth America	Sales		2000000North America	Expenses	1250000North America	Taxes		250000North America	Profit		500000Europe		Sales		2500000Europe		Expenses	1250000Europe		Taxes		500000Europe		Profit		750000South America	Sales		500000South America	Expenses	250000Asia		Sales		800000Asia		Expenses	350000Asia		Taxes		100000SELECT COUNTRY,SUM(CASE WHEN Type='SALES' THEN COST END) AS SALESSUM(CASE WHEN Type='Expenses' THEN COST END) AS ExpensesSUM(CASE WHEN Type='Taxes' THEN COST END) AS TaxesSUM(CASE WHEN Type='Profit' THEN COST END) AS ProfitFROM tbl_country_costsGROUP BY COUNTRYWITH ROLLUP COUNTRYThat gives me:Country 	Sales 	Expenses	Taxes 	ProfitNorth America	2000000	1250000 	250000 	500000Europe 		2500000	1250000 	500000 	750000South America	500000 	250000 		null 	nullAsia 		800000 	350000 		100000 	null		5800000 3100000         850000  1250000   <-- this is the totals for each columnQuestion is how do I make it so that i'm doing a horizontal sum also? For example:Country 	Sales 	Expenses	Taxes 	Profit   TotalNorth America	2000000	1250000 	250000 	500000   4000000  <-- total for the rowEurope 		2500000	1250000 	500000 	750000   5000000  <-- total for the rowSouth America	500000 	250000 		null 	null     750000   <-- total for the rowAsia 		800000 	350000 		100000 	null     1250000  <-- total for the row		5800000 3100000         850000  1250000    Also the aggregate function does not have to be sum.  It could min, max, or avg.Thank you,-Tesh |  |  
                                    | RickDSlow But Sure Yak Herding Master
 
 
                                    3608 Posts | 
                                        
                                          |  Posted - 2009-09-15 : 04:45:18 
 |  
                                          | Just add SUM(COST) as Total to your query. This will be the total as you are grouping by country already. |  
                                          |  |  |  
                                    | caliguy1979Starting Member
 
 
                                    2 Posts | 
                                        
                                          |  Posted - 2009-09-15 : 12:31:03 
 |  
                                          | Thanks Rick. That works.  Glad it was that simple to do. |  
                                          |  |  |  
                                |  |  |  |