Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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
在SQLServer2008中使用PIVOT_Sql_Sql Server_Tsql_Sql Server 2008_Pivot - Fatal编程技术网

在SQLServer2008中使用PIVOT

在SQLServer2008中使用PIVOT,sql,sql-server,tsql,sql-server-2008,pivot,Sql,Sql Server,Tsql,Sql Server 2008,Pivot,假设我在SQL Server 2008表或[table]类型的变量中有一些数据: author_id review_id question_id answer_id 88540 99001 1 719 88540 99001 2 720 88540 99001 3 721 88540

假设我在SQL Server 2008表或[table]类型的变量中有一些数据:

author_id     review_id     question_id     answer_id
88540         99001         1               719
88540         99001         2               720
88540         99001         3               721
88540         99001         4               722
88540         99001         5               723
36414         24336         1               302
36414         24336         2               303
36414         24336         3               304
36414         24336         4               305
36414         24336         5               306
我想将数据检索为如下所示的结果集:

author_id     review_id     1     2     3     4     5
88540         99001         719   720   721   722   723
36414         24336         302   303   304   305   306
SELECT author_id, review_id, [1], [2], [3], [4], [5]
FROM 
    (
        SELECT author_id, review_id, question_id, answer_id
        FROM the_table
    ) up
PIVOT (MAX(answer_id) FOR question_id IN ([1],[2],[3],[4],[5])) AS pvt
无论如何,我怀疑PIVOT操作符是我所需要的,但我不知道如何开始,尤其是当表中的question_id行数可能不同时。在上面的示例中是5,但在另一个查询中,该表可能会填充7个不同的问题

select * 
from @t pivot
(
    max(answer_id) for question_id in ([1],[2],[3],[4],[5])
) pivotT

改变列表[1]、[2]、[3]、[4]、[5]的唯一方法是在字符串中动态构建此查询,然后执行它。

实际上,最好在客户端执行此操作。假设您正在使用RealServices,根据第一个结果集获取数据,并使用矩阵、在行组中的AuthIdID和ReVIEW.ID、列组中的QuasiSID和中间的Max RealthyId显示它。

查询是可行的,但您现在需要动态SQL

…类似于:

DECLARE @QuestionList nvarchar(max);
SELECT @QuestionList = STUFF(
(SELECT ', ' + quotename(question_id)
FROM YourTable
GROUP BY question_id
ORDER BY question_id
FOR XML PATH(''))
, 1, 2, '');

DECLARE @qry nvarchar(max);
SET @qry = '
SELECT author_id, review_id, ' + @QuestionList + 
FROM (SELECT author_id, review_id, question_id, answer_id
      FROM YourTable
     ) 
PIVOT
(MAX(AnswerID) FOR question_id IN (' + @QuestionList + ')) pvt
ORDER BY author_id, review_id;';

exec sp_executesql @qry;
你有很好的例子和解释

在您的情况下,它将是这样的:

author_id     review_id     1     2     3     4     5
88540         99001         719   720   721   722   723
36414         24336         302   303   304   305   306
SELECT author_id, review_id, [1], [2], [3], [4], [5]
FROM 
    (
        SELECT author_id, review_id, question_id, answer_id
        FROM the_table
    ) up
PIVOT (MAX(answer_id) FOR question_id IN ([1],[2],[3],[4],[5])) AS pvt


基本上,您可以预先检查数据以获取列,然后使用动态透视列表动态生成SQL。实际上没有非动态方式,因为要返回的集合中的列的定义是不固定的。

这似乎是我需要的。我会给它一个机会,并报告回来-谢谢!请记住子查询。如果您只是从表中使用SELECT*,那么涉及的任何其他列都将影响PIVOT函数提供的隐式分组。如果您有任何错误,请注释掉exec行,将其替换为select@qryI不再使用[select*],我总是明确说明我访问的列,这样就不会有问题了。是的,我使用[select@qry]有一段时间了,以便在运行之前查看/调试generatesql语句。您的代码按照承诺工作-非常感谢您的帮助!
SELECT author_id, review_id, [1], [2], [3], [4], [5]
FROM 
    (
        SELECT author_id, review_id, question_id, answer_id
        FROM the_table
    ) up
PIVOT (MAX(answer_id) FOR