SQL(Azure SQL)中的PIVOT遇到问题

时间:2019-04-17 21:45:04

标签: sql sql-server pivot azure-sql-database

我需要在一行中列出调查答复。调查答复表列出了一个questionID和一个ResponseID(这些是多项选择题),因此每个答复一行。有12个问题。答复日期,进行调查的工作人员和进入调查的工作人员之类的内容都保存在其他表格中。

因此,我有一个查询,该查询将一项调查的响应分为12行。现在,我需要将所有内容合并为一行。枢轴,对吗?

但是我永远无法使它工作。 :-(在此论坛和其他论坛中尝试了几种解决方案(包括此处的Mickey文档:https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-2017)。

然后我找到了这个根本不使用数据透视的解决方案,这里:SQL Pivot Table Grouping

效果很好,但是该示例只有两个问题。我们的某些调查将涉及50多个问题,因此我想这将不是一个很好的解决方案。

所以我回到我的关键问题上。

经验水平介于白痴和新手之间,所以我可能缺少明显的东西。

这是查询本身(按预期工作):

SELECT AssessmentResponses.ID, AssessmentQuestions.QuestionNumber, 
   AssessmentResponseAnswers.QuestionID,
   AssessmentAnswerChoices.AnswerChoiceNumber 
FROM (AssessmentResponses RIGHT JOIN AssessmentResponseAnswers 
   ON AssessmentResponses.ID = AssessmentResponseAnswers.AssessmentResponseID) 
   LEFT JOIN (AssessmentQuestions RIGHT JOIN AssessmentAnswerChoices 
   ON AssessmentQuestions.ID = AssessmentAnswerChoices.AssessmentQuestionID) 
   ON AssessmentResponseAnswers.AnswerChoiceID = AssessmentAnswerChoices.AnswerChoiceID 
WHERE AssessmentResponses.AssessmentID = 1 AND AssessmentResponses.RespondentID = 44;

这是我尝试使其枢纽的方法:

SELECT ID, [1A], [1B], [2A], [2B], [3A], [3B], [4A], [4B], [5A], [5B], [6A], [6B] 
FROM (
SELECT AssessmentResponses.ID, AssessmentQuestions.QuestionNumber, 
   AssessmentResponseAnswers.QuestionID,
   AssessmentAnswerChoices.AnswerChoiceNumber 
FROM (AssessmentResponses RIGHT JOIN AssessmentResponseAnswers 
   ON AssessmentResponses.ID = AssessmentResponseAnswers.AssessmentResponseID) 
   LEFT JOIN (AssessmentQuestions RIGHT JOIN AssessmentAnswerChoices 
   ON AssessmentQuestions.ID = AssessmentAnswerChoices.AssessmentQuestionID)
   ON AssessmentResponseAnswers.AnswerChoiceID = AssessmentAnswerChoices.AnswerChoiceID 
WHERE AssessmentResponses.AssessmentID = 1 AND AssessmentResponses.RespondentID = 44
) AS Src 
PIVOT
(
MAX(AnswerChoiceNumber) 
FOR QuestionNumber IN ([1A], [1B], [2A], [2B], [3A], [3B], [4A], [4B], [5A], [5B], [6A], [6B]) 
)
AS Pvt;

我希望能给我1行13列(ID加12个问题)。但是它仍然给了我12行:13列在那里,并且只给出了12个问题中11个的空值。 (在第1行中,1A有一个答案;在第2行中,1B有一个答案,等等。)

我想念什么?

2 个答案:

答案 0 :(得分:0)

在我看来,这并不是与透视相关的工作。也许像这样:

SELECT ar.ID
,(
  SELECT aac.AnswerChoiceNumber
  FROM AssessmentResponseAnswers ara, AssessmentAnswerChoices aac, AssessmentQuestions aq
  WHERE ar.ID=ara.AssessmentResponseID
  AND ara.AnswerChoiceID=aac.AnswerChoiceID
  AND aq.ID=aac.AssessmentQuestionID
  AND aq.QuestionNumber='1A'
 ) 1A
 ,(
  SELECT aac.AnswerChoiceNumber
  FROM AssessmentResponseAnswers ara, AssessmentAnswerChoices aac, AssessmentQuestions aq
  WHERE ar.ID=ara.AssessmentResponseID
  AND ara.AnswerChoiceID=aac.AnswerChoiceID
  AND aq.ID=aac.AssessmentQuestionID
  AND aq.QuestionNumber='1B'
 ) 1B
, (...)
FROM AssessmentResponses ar
WHERE ar.AssessmentID=1 
AND ar.RespondentID=44

我在使用联接时遇到问题,但我试图弄清楚-也许是正确的。

答案 1 :(得分:0)

稍微调整一下代码即可摆脱子查询中的“ QuestionID”。它与您的工作无关,因此SQL Server会认为这是您的关键之一。

SELECT ID, [1A], [1B], [2A], [2B], [3A], [3B], [4A], [4B], [5A], [5B], [6A], [6B] 
FROM (
SELECT AssessmentResponses.ID, AssessmentQuestions.QuestionNumber, 
   AssessmentAnswerChoices.AnswerChoiceNumber 
FROM (AssessmentResponses RIGHT JOIN AssessmentResponseAnswers 
   ON AssessmentResponses.ID = AssessmentResponseAnswers.AssessmentResponseID) 
   LEFT JOIN (AssessmentQuestions RIGHT JOIN AssessmentAnswerChoices 
   ON AssessmentQuestions.ID = AssessmentAnswerChoices.AssessmentQuestionID)
   ON AssessmentResponseAnswers.AnswerChoiceID = AssessmentAnswerChoices.AnswerChoiceID 
WHERE AssessmentResponses.AssessmentID = 1 AND AssessmentResponses.RespondentID = 44
) AS Src 
PIVOT
(
MAX(AnswerChoiceNumber) 
FOR QuestionNumber IN ([1A], [1B], [2A], [2B], [3A], [3B], [4A], [4B], [5A], [5B], [6A], [6B]) 
)
AS Pvt;

我想你已经习惯了设计师。一些技巧(上面未实现)使代码更具可读性:

  • 通常可以在连接语句周围省略括号
  • 我会重新整理您的联接,以使每个联接语句关联一个陈述式
  • 别名!