将行更改为SQL Server中的列

时间:2016-02-10 02:45:46

标签: sql sql-server pivot

我已经在这个查询上工作了一个半小时,但我无法完成,

首先,这是我的问题:

SELECT  
    Questions, PossibleAnswer, 
    ((COUNT(PossibleAnswer) + 0.0) / 10 ) * 100 AS Percentage 
FROM  
    (SELECT  
         A.AnswerID, B.Questions, B.QuestionID, C.PossibleAnswer  
     FROM 
         TblSurveyCustomerAnswers A  
     INNER JOIN 
         TblSurveyQuestion B ON A.QuestionID = B.QuestionID 
     INNER JOIN 
         TblSurveyAnswer C ON A.AnswerID = C.AnswerID 
     WHERE 
         A.CustomerID = 1) AS SOURCE  
GROUP BY 
    Questions, PossibleAnswer 

结果如下:

enter image description here

现在,我希望列名PossibleAnswer的行在列中进行转换,因此我进行了研究并找到了PIVOT命令(我需要动态,因为它是可能的答案字段)这是我的代码

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
                    + QUOTENAME(PossibleAnswer)
FROM 
(
  SELECT DISTINCT X.* 
  FROM 
  (
    SELECT Questions,PossibleAnswer, ((COUNT(PossibleAnswer) + 0.0) / 10 ) * 100 AS Percentage 
    FROM  
       (  
          SELECT  A.AnswerID,B.Questions, B.QuestionID, C.PossibleAnswer  
          FROM TblSurveyCustomerAnswers A  
               INNER JOIN TblSurveyQuestion B  
                       ON A.QuestionID = B.QuestionID 
               INNER JOIN TblSurveyAnswer C 
                       ON A.AnswerID = C.AnswerID 
          WHERE A.CustomerID = 1 
       ) AS SOURCE  
    GROUP BY Questions, PossibleAnswer 
  ) X  
) AS B

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
    'SELECT Questions, ' + @ColumnName + '
     FROM TblSurveyCustomerAnswers A  
          INNER JOIN TblSurveyQuestion B  
                  ON A.QuestionID = B.QuestionID 
          PIVOT(Max(Questions) 
                FOR PossibleAnswer IN (' + @ColumnName + ')) AS PVTTable'

--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

我无法获得枢纽工作,需要帮​​助。我被卡住了。看到这个错误:

enter image description here

2 个答案:

答案 0 :(得分:2)

一般来说,对于这些问题,你应该提供样本数据,表格定义和预期输出,以便人们可以使用你的脚本,摆弄它并产生有效的东西。有关这种方法,请参阅How to post a T-SQL question on a public forum

由于很难看到动态脚本,没有表结构,并指出你的问题是什么,让我给你以下建议:

  1. 首先将该查询的输出插入到临时表中,而不是使用生成输出的大查询并围绕该大查询形成查询。您可以通过在INTO #temp_table子句后放置SELECT子句来完成此操作。这将创建一个包含查询输出的新临时表#temp_table
  2.     SELECT --your select columns
        INTO #p_in -- creates a temporary table #p_in that contains the output
        FROM --the rest of your query
    
    1. 根据新创建的临时表确定数据透视表列。它会更加简洁,更容易发现错误

    2. 使用临时表编写动态SQL(同样,它会更加精简,更容易发现错误)

    3. 执行动态SQL后,不要忘记DROP临时表。

答案 1 :(得分:0)

我只是尝试在没有临时表的情况下解决问题。您可以根据需要编辑查询。

--For PIVOT column

DECLARE @ColumnName AS NVARCHAR(MAX)

SELECT   @ColumnName ''''+ PossibleAnswer + '''' + ' , ' + @ColumnName
FROM 
(
    SELECT  
        DISTINCT PossibleAnswer
    FROM  
    (
        SELECT  
             A.AnswerID, B.Questions, B.QuestionID, C.PossibleAnswer  
         FROM 
             TblSurveyCustomerAnswers A  
         INNER JOIN 
             TblSurveyQuestion B ON A.QuestionID = B.QuestionID 
         INNER JOIN 
             TblSurveyAnswer C ON A.AnswerID = C.AnswerID 
         WHERE 
             A.CustomerID = 1
    ) AS SOURCE  
)B

- 删除最后一个逗号

 IF @ColumnName != ''
   BEGIN
       SET @ColumnName = SUBSTRING(@ColumnName, 1, LEN(@ColumnName)-1)
   END

- 制作结果

SELECT * 
FROM
(
    SELECT Questions,PossibleAnswer, ((COUNT(PossibleAnswer) + 0.0) / 10 ) * 100 AS Percentage 
    FROM  
       (  
          SELECT  A.AnswerID,B.Questions, B.QuestionID, C.PossibleAnswer  
          FROM TblSurveyCustomerAnswers A  
               INNER JOIN TblSurveyQuestion B  
                       ON A.QuestionID = B.QuestionID 
               INNER JOIN TblSurveyAnswer C 
                       ON A.AnswerID = C.AnswerID 
          WHERE A.CustomerID = 1 
       ) AS SOURCE  
    GROUP BY Questions, PossibleAnswer 
)C
PIVOT
( Max(Questions) 
       FOR PossibleAnswer IN (@ColumnName)
) AS PVTTable