時間:2024-02-05 11:03作者:下載吧人氣:13
insert into #table1 ( id,code, name ) values ( 1, ‘m1′,’a’ ), ( 2, ‘m2’,null ), ( 3, ‘m3’, ‘c’ ), ( 4, ‘m2′,’d’ ), ( 5, ‘m1′,’c’ );
go
select * from #table1;
–方法一(推薦)
select PVT.code, PVT.a, PVT.b, PVT.c
from #table1 pivot(count(id) for name in(a, b, c)) as PVT;
–方法二
with P as (select * from #table1)
select PVT.code, PVT.a, PVT.b, PVT.c
from P pivot(count(id) for name in(a, b, c)) as PVT;
drop table #table1;
網(wǎng)友評論