如下的数据
DECLARE @A table( type char(10),counts int )
insert into @a
select 'Type1', 10
union all
select 'Type1', 13
union all
select 'type2', 20
union all
select 'type2', 2
union all
select 'type3', 30
union all
select 'type3', 17
select * from @a
type | counts |
Type1 | 10 |
Type1 | 13 |
type2 | 20 |
type2 | 2 |
type3 | 30 |
type3 | 17 |
行列转换:
select * from @a
PIVOT
(sum(counts)
FOR type IN ([Type1],[type2],[type3])
)b
结果集:
Type1 | type2 | type3 |
23 | 22 | 47 |