如何在sql server 2005中将行值转换为列

时间:2011-06-03 06:42:26

标签: sql-server-2005 tsql

如何将行转换为列。我有以下结果集。

UserID  VendorName  QuestionText AnswerText
1         KK         abc1         ans1
1         KK         abc2         ans2
1         KK         abc3         ans3
2         JJ         abc1         ans2
2         JJ         abc2         ans3
2         JJ         abc3         ans1   

OUTPUT应为:

UserID VendorName   abc1    abc2   abc3
1             KK    ans1    ans2   ans3
2             JJ    ans2    ans3   ans1

感谢。

2 个答案:

答案 0 :(得分:2)

SELECT grsid,contactid, [1] rejectcode, [12] rejectcode, [89] rejectcode
FROM **rjct1**
PIVOT (MAX(rejectcode)
    FOR rejectcode IN ([1] , [12], [89])
) AS pvt

答案 1 :(得分:1)

SELECT UserID, VendorName, [abc1], [abc2], [abc3]
FROM Questions
PIVOT (MAX(AnswerText)
    FOR QuestionText IN ([abc1], [abc2], [abc3])
) AS pvt