首页 > 代码库 > 行转列

行转列

select * from  (  select   case  

                                  when    A  then  a

                                  when    B  then  b

                                  when    C   then c

                        end    字段

                      from  表名

 )tb  pivot(max (字段) for  字段 in(a,b,c))a  order by 字段1 desc

 

 

select * from (select Year,Time,ID1,Name,
case
when substring(ID,5,2) = ‘14‘ then ‘A‘
when substring(ID,5,2) = ‘15‘ then ‘A‘
when substring(ID,5,2) = ‘04‘ then ‘B‘
when substring(ID,5,2) = ‘25‘ then ‘C‘
when substring(ID,5,2) = ‘26‘ then ‘D‘
end ID
from census_Detail
where Year=@strYear AND Time=@Times

)tb pivot( max(ID) FOR ID IN (A,B,C,D))a order by ID1 desc

ABCD可以根据需要在前台绑定只是一个栏位名

行转列