declare @table as table(ProductName varchar(20),Quant int,Val int)
insert into @table values('Product 1',2,30)
insert into @table values('Product 1',4,60)
insert into @table values('Product 2',2,10)
insert into @table values('Product 2',4,20)
insert into @table values('Product 3',1,20)
insert into @table values('Product 3',1,20)
select ProductName,isnull(convert(varchar,Quant),'Total:') Quant,Val
from (select ProductName,Quant,sum(Val) as Val from @table group by ProductName,Quant with rollup) as t1
where ProductName is not null
/*
Product 1 2 30
Product 1 4 60
Product 1 Total: 90
Product 2 2 10
Product 2 4 20
Product 2 Total: 30
Product 3 1 40
Product 3 Total: 40
*/
|