0

我有以下数据库设计:

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

但它的问题并没有显示部门。它应该显示每个问题的每个部门的参与者数量及其所有答案,即使其中一个答案没有参与者。

4

1 回答 1

3

QuestionAnswers 是获取答案所需的表,但由于您使用的是INNER JOIN从 CompleteSurvey 到 QuestionAnswers 的表格,因此您正在消除 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

但是,我建议您切换您加入的顺序,以下将消除一个外部联接,总体上它的性能不如内部联接。当使用左连接而不是右连接时,它还(在我的选项中)使查询更容易阅读。

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

更新要求

如果我正确理解要求,您只需加入 Employee 和 Department 以 questionAnswer 即可获得部门。这可以通过左加入两次来完成,但是我将它移到子查询中只是因为我喜欢在我可以使用的地方使用 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
于 2012-05-21T08:51:47.573 回答