use cube or rollup Declare @myFruit table( id int, fruitType nvarchar(12), Qty tinyint, owner nvarchar(12))Insert into @myFruit Select 3,'Apple',3,'Joe' union Select 7,'Orange',2,'Joe' union Select 4,'Apple',4,'Sam' union Select 9,'Apple',1,'Joe' union Select 2,'Orange',5,'Sam'SELECT FruitType,Owner, sum(Qty)FROM @myFruitGROUP BY FruitType,Owner WITH Rollup --orSELECT FruitType,Owner, sum(Qty)FROM @myFruitGROUP BY FruitType,Owner WITH CUBE
ResultsFruitType Owner ------------ ------------ ----------- Apple Joe 4Apple Sam 4Apple NULL 8 total applesOrange Joe 2Orange Sam 5Orange NULL 7 total orangesNULL NULL 15 total fruits --orFruitType Owner ------------ ------------ ----------- Apple Joe 4Apple Sam 4Apple NULL 8 total applesOrange Joe 2Orange Sam 5Orange NULL 7 total orangesNULL NULL 15 total fruitsNULL Joe 6 total Joe's fruitNULL Sam 9 total Same's fruit
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*A candle loses nothing by lighting another candle