我想将表的每一行转换为不同的列,使一个特定的行作为转置表的标题。请尽快帮助我。任何简单的例子都可以。
答案 0 :(得分:0)
使用PIVOT
:
select
--userID, Stage1, Stage2, Stage3, Stage4
*
from (
select '1' as UserID,'1' as Stage,'1-1-2013' as [Date] union all
select '1','2','2-1-2013' union all
select '2','1','1-1-2013' union all
select '1','3','5-1-2013' union all
select '2','2','3-1-2013' union all
select '3','1','6-1-2013' union all
select '3','2','8-1-2013' union all
select '1','4','10-1-2013' union all
select '3','3','12-1-2013'
) x
pivot (
max([Date]) for Stage in ([1],[2],[3],[4],[5],[6])
) p
有关此主题的更多信息: http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/