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 |
thephill
Starting Member
2 Posts |
Posted - 2015-03-02 : 20:15:16
|
hi,im using SQL server 2008 R2 developer for this, the following sql statement was used on Product and ProductCategory table on AdventureWorksLT2008R2 database. select p.Color, SUM(p.ListPrice) as total, pc.Name from SalesLT.Product p inner join SalesLT.ProductCategory pc on p.ProductCategoryID=pc.ProductCategoryIDgroup by p.Color,pc.Name and it does grouping like thisMulti 269.97 Bib-ShortsNULL 120.00 Bike RacksNULL 159.00 Bike StandsNULL 23.97 Bottles and CagesNULL 276.72 Bottom BracketsSilver 213.00 BrakesMulti 8.99 CapsSilver 20.24 ChainsNULL 7.95 CleanersBlack 836.97 CranksetsSilver 212.95 DerailleursNULL 21.98 FendersNULL 553.20 ForksBlack 187.44 GlovesNULL 591.12 HandlebarsNULL 261.22 HeadsetsBlack 34.99 HelmetsBlue 34.99 HelmetsRed 34.99 HelmetsSilver 54.99 Hydration PacksMulti 199.96 JerseysYellow 215.96 JerseysNULL 93.97 LightsNULL 25.00 LocksBlack 27404.84 Mountain BikesSilver 26462.84 Mountain BikesBlack 9391.99 Mountain FramesSilver 9599.11 Mountain FramesGrey 125.00 PanniersSilver/Black 448.13 PedalsNULL 44.98 PumpsBlack 16631.30 Road BikesRed 39652.64 Road BikesYellow 12406.41 Road BikesBlack 9180.82 Road FramesRed 13586.47 Road FramesYellow 2974.15 Road FramesNULL 356.70 SaddlesBlack 449.93 ShortsWhite 36.98 SocksBlack 224.97 TightsNULL 214.31 Tires and TubesBlue 18107.43 Touring BikesYellow 13248.03 Touring BikesBlue 5682.74 Touring FramesYellow 5682.74 Touring FramesBlue 190.50 VestsBlack 3093.01 Wheelsbut as you can see there are repeated rows of each color i.e black was repeated several times and so is blue etc. What i want to do is add all total of black and put it under just one black and add all totals of blue and put it under just one blue, this can be done using table variables but is there another way to do this? thanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2015-03-03 : 00:42:17
|
GROUP BY GROUPING SETS ( (Color, Name), (Color) ) Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
ScottPletcher
Aged Yak Warrior
550 Posts |
Posted - 2015-03-03 : 11:14:14
|
select p.Color, SUM(p.ListPrice) as totalfrom SalesLT.Product p group by p.Color--order by Color |
|
|
|
|
|
|
|