我有以下数据库设计:
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
但它的问题并没有显示部门。它应该显示每个问题的每个部门的参与者数量及其所有答案,即使其中一个答案没有参与者。