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 |
|
funk.phenomena
Posting Yak Master
121 Posts |
Posted - 2011-07-21 : 11:57:13
|
| Hi All - I have a "Sales" table which has sales totals for each store.CHAIN ADDRESS SALESTARGET 12 MAIN ST $1,003.07TARGET 4079 BROADWAY $10,101.49TARGET 749 LINCOLN AVE $5,978.02WALGREENS 123 BIRCH $543.99WALGREENS 9779 SPARROW $54.97WALGREENS 49 BAYWOOD $67.97SELECT [CHAIN], [ADDRESS], SUM([SALES]) FROM SALESTABLEGROUP BY [CHAIN], [ADDRESS]I would like the results to subtotal each "chain", as well as display the address as well, so that results are displayed as follows:TARGET 12 MAIN ST $1,003.07TARGET 4079 BROADWAY $10,101.49TARGET 749 LINCOLN AVE $5,978.02TARGET TOTAL $17082.58WALGREENS 123 BIRCH 543.99WALGREENS 9779 SPARROW $54.97WALGREENS 49 BAYWOOD $67.97WALGREENS TOTAL $666.93 GRAND TOTAL $17749.51 Can anyone advise on how this can be done? THANKS! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-21 : 12:02:43
|
| use WITH CUBE to get subtotals. alternatively if its a reporting application (SSRS) you can get it very easily by using a group footer------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
pduffin
Yak Posting Veteran
68 Posts |
Posted - 2011-07-22 : 17:34:47
|
| As long as Sales are not negative this should work for youselect chain,[ADDRESS],sum(sales) as Salesfrom #SALESTABLEgroup by chain,[ADDRESS]union allselect chain,rtrim(Chain) + ' Total',sum(Sales)from #SALESTABLEgroup by chainorder by chain,sum(Sales) |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-07-23 : 11:05:03
|
| Look at the ROLLUP option in GROUP BY--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
|
|
|
|
|