Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 如何在SQLServer2005中将行值转换为列_Sql Server 2005_Tsql - Fatal编程技术网

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

Sql server 2005 如何在SQLServer2005中将行值转换为列,sql-server-2005,tsql,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

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

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   
输出应为:

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

谢谢。

在这里,这已经被询问和回答了至少无数次了,所以-我建议搜索“SQL Server PIVOT”-你应该有很多答案!重复我尝试使用pivot,但它需要代码块内的任何聚合函数(count,sum)。但我的列AnswerText的文本值不是任何整数。那么如何使用PIVOT来实现它。
SELECT UserID, VendorName, [abc1], [abc2], [abc3]
FROM Questions
PIVOT (MAX(AnswerText)
    FOR QuestionText IN ([abc1], [abc2], [abc3])
) AS pvt
SELECT grsid,contactid, [1] rejectcode, [12] rejectcode, [89] rejectcode
FROM **rjct1**
PIVOT (MAX(rejectcode)
    FOR rejectcode IN ([1] , [12], [89])
) AS pvt