我已经在这个查询上工作了一个半小时,但我无法完成,
首先,这是我的问题:
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
结果如下:
现在,我希望列名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
我无法获得枢纽工作,需要帮助。我被卡住了。看到这个错误:
答案 0 :(得分:2)
一般来说,对于这些问题,你应该提供样本数据,表格定义和预期输出,以便人们可以使用你的脚本,摆弄它并产生有效的东西。有关这种方法,请参阅How to post a T-SQL question on a public forum。
由于很难看到动态脚本,没有表结构,并指出你的问题是什么,让我给你以下建议:
INTO #temp_table
子句后放置SELECT
子句来完成此操作。这将创建一个包含查询输出的新临时表#temp_table
。 SELECT --your select columns
INTO #p_in -- creates a temporary table #p_in that contains the output
FROM --the rest of your query
根据新创建的临时表确定数据透视表列。它会更加简洁,更容易发现错误
使用临时表编写动态SQL(同样,它会更加精简,更容易发现错误)
执行动态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