create table #temp
(
id nvarchar(2),
[Type] nvarchar(2),
value int
)
insert into #temp values ('a',1,20)
insert into #temp values ('b',1,30)
insert into #temp values ('c',2,40)
insert into #temp values ('d',2,50)
;with temp1 as
(
select
[type] [key]
,sum(value) value
from #temp
group by [type]
),
temp2 as
(
select
id [key]
,sum(value) value
from #temp
group by id
)
select * from temp1
union all
select * from temp2 drop table #temp
--sql 2008测试通过