日期:2014-05-17 浏览次数:20541 次
select
[1],
[A]=max(case when [column1]='2' then [column2] else 0 end),
[B]=max(case when [column1]='3' then [column2] else 0 end),
[C]=max(case when [column1]='4' then [column2] else 0 end),
[D]=max(case when [column1]='5' then [column2] else 0 end)
from
T
group by [1]
select 2 id,1 A,2 B,3 C,4 D,5 E,6 F
into #t
union all select 3 ,1,2,3,4,5,6
union all select 4 ,1,2,3,4,5,6
union all select 5 ,1,2,3,4,5,6
select * from #t
--查询
select name,max([2]) [2]
,max([3]) [3]
,max([4]) [4]
,max([5]) [5]
from
(
select 'A' NAME,case when id=2 then A end as '2'
,case when id=3 then A end as '3'
,case when id=4 then A end as '4'
,case when id=5 then A end as '5'
from #t
union all
select 'B',case when id=2 then B end as '2'
,case when id=3 then B end as '3'
,case when id=4 then B end as '4'
,case when id=5 then B end as '5'
from #t
union all
select 'C',case when id=2 then C end as '2'
,case when id=3 then C end as '3'
,case when id=4 then C end as '4'
,case when id=5 then C end as '5'
from #t
union all
select 'D',case when id=2 then D end as '2'
,case when id=3 then D end as '3'
,case when id=4 then D end as '4'
,case when id=5 then D end as '5'
from #t