我有以下数据库设计:
Employee Table: EmployeeID, Name, OrgCode
Department Table: OrgCode, DepartName
CompleteSurvey Table: ID, RespondantID, QuestionsAnswersID
Questions Table: QuestionID, Question
Answers Table: AnswerID, Answer
QuestionsAnswers Table: ID, QuestionID, AnswerID
每个问题都有不同的多种选择。大多数问题都有相同的选择,例如(同意,不同意)。我想写一个查询,显示问题及其所有选项和每个选项中的参与者数量,即使它是零。 我写了查询,它显示了问题的所有答案,即使没有人选择其中一个答案,但查询在问题列中显示NULL,除了没有参与者的答案,我不知道为什么的。有什么帮助吗?
我的查询:
SELECT COUNT(DISTINCT dbo.CompleteSurvey.RespondantID) AS [Total Number of Participants], dbo.Answers.Answer, dbo.Questions.Question
FROM dbo.CompleteSurvey INNER JOIN
dbo.QuestionsAnswers ON dbo.CompleteSurvey.QuestionsAnswersID = dbo.QuestionsAnswers.ID INNER JOIN
dbo.Questions ON dbo.QuestionsAnswers.QuestionID = dbo.Questions.QuestionID RIGHT OUTER JOIN
dbo.Answers ON dbo.QuestionsAnswers.AnswerID = dbo.Answers.AnswerID
GROUP BY dbo.Answers.Answer, dbo.Questions.Question
更新
查询应显示问题中每个部门的参与者人数及其所有答案,那么该如何做?
我的更新查询:
SELECT COUNT(DISTINCT dbo.CompleteSurvey.RespondantID) AS [Total Number of Participants], dbo.Answers.Answer, dbo.Questions.Question,
dbo.Departments.DepartmentName
FROM dbo.Employees INNER JOIN
dbo.CompleteSurvey ON dbo.Employees.EmployeeID = dbo.CompleteSurvey.RespondantID INNER JOIN
dbo.Departments ON dbo.Employees.OrgCode = dbo.Departments.OrgCode RIGHT OUTER JOIN
dbo.Questions INNER JOIN
dbo.QuestionsAnswers ON dbo.Questions.QuestionID = dbo.QuestionsAnswers.QuestionID INNER JOIN
dbo.Answers ON dbo.QuestionsAnswers.AnswerID = dbo.Answers.AnswerID ON dbo.CompleteSurvey.QuestionsAnswersID = dbo.QuestionsAnswers.ID
GROUP BY dbo.Answers.Answer, dbo.Questions.Question, dbo.Departments.DepartmentName
但问题不在于部门。它应该显示每个问题中每个部门的参与者数量及其所有答案,即使其中一个答案没有参与者。
答案 0 :(得分:3)
QuestionAnswers是获取答案所需的表格,但由于您使用的是从CompleteSurvey到QuestionAnswers的INNER JOIN
,因此您将删除QuestionAnswers中的所有行,而CompleteSurvey中没有相应的行。我认为如果您将它们之间的连接更改为RIGHT OUTER
,这将有效。
SELECT COUNT(DISTINCT dbo.CompleteSurvey.RespondantID) AS [Total Number of Participants],
dbo.Answers.Answer,
dbo.Questions.Question
FROM dbo.CompleteSurvey
RIGHT OUTER JOIN dbo.QuestionsAnswers
ON dbo.CompleteSurvey.QuestionsAnswersID = dbo.QuestionsAnswers.ID
INNER JOIN dbo.Questions
ON dbo.QuestionsAnswers.QuestionID = dbo.Questions.QuestionID
RIGHT OUTER JOIN dbo.Answers
ON dbo.QuestionsAnswers.AnswerID = dbo.Answers.AnswerID
GROUP BY dbo.Answers.Answer, dbo.Questions.Question
然而我建议您切换要加入的订单,下面将删除一个OUTER JOIN,它总体上不如INNER JOIN那样好。当使用LEFT JOIN而不是右连接时,它(在我的选项中)也使得查询更容易。
SELECT COUNT(DISTINCT dbo.CompleteSurvey.RespondantID) AS [Total Number of Participants],
dbo.Answers.Answer,
dbo.Questions.Question
FROM dbo.Questions
INNER JOIN dbo.QuestionAnswers
ON Questions.QuestionID = QuestionAnswers.QuestionID
INNER JOIN dbo.Answers
ON QuestionAnswers.AnswerID = Answers.AnswerID
LEFT JOIN dbo.CompleteSurvey
ON QuestionAnswers.QuestionAnswersID = CompleteSurvey.QuestionAnswersID
GROUP BY Answers.Answer, Questions.Question
更新要求
如果我正确理解了这些要求,您只需要加入员工和部门来质疑,以便获得该部门。这可以通过左连接两次来完成,但是我把它移动到子查询除了我喜欢在我可以使用INNER JOIN之外没有其他原因,所以如果有一种方法我可以用更少的OUTER JOIN重新排列查询我会做到这一点:
SELECT COUNT(DISTINCT CompleteSurvey.RespondantID) AS [Total Number of Participants],
Answers.Answer,
Questions.Question,
DepartName
FROM dbo.Questions
INNER JOIN dbo.QuestionsAnswers
ON Questions.QuestionID = QuestionsAnswers.QuestionID
INNER JOIN dbo.Answers
ON QuestionsAnswers.AnswerID = Answers.AnswerID
CROSS JOIN Departments
LEFT JOIN
( SELECT OrgCode, QuestionsAnswersID, RespondantID
FROM dbo.CompleteSurvey
INNER JOIN dbo.Employees
ON EmployeeID = RespondantID
) CompleteSurvey
ON QuestionsAnswers.ID = CompleteSurvey.QuestionsAnswersID
AND Departments.OrgCode = CompleteSurvey.OrgCode
GROUP BY Answers.Answer, Questions.Question, DepartName
ORDER BY Questions.Question, Answers.Answer, DepartName