行转列

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可以根据需要在前台绑定只是一个栏位名

posted @ 2016-09-20 09:15  wonderfulviews  阅读(163)  评论(0编辑  收藏  举报