sql一列变多列

一列变三列的例子:
select  
max ( case  rownumber  %   3   when   2   then  id  else   null   end as  id1,
max ( case  rownumber  %   3   when   2   then   [ text ]   else   null   end as  text1,
max ( case  rownumber  %   3   when   1   then  id  else   null   end as  id2,
max ( case  rownumber  %   3   when   1   then   [ text ]   else   null   end as  text2,
max ( case  rownumber  %   3   when   0   then  id  else   null   end as  id3,
max ( case  rownumber  %   3   when   0   then   [ text ]   else   null   end as  text3
from  
(
select  row_number()  over ( order   by   [ type ]   desc as  rownumber, [ id ] , [ text ]   from  tablename) A
group   by  (rownumber + 2 ) / 3
变多列以此类推

你可能感兴趣的:(sql)