我有以下数据库设计:
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
我写了一个查询,用所有可能的答案来显示每个问题,以及每个问题参与者的总数,即使有一个可能的答案,没有任何参与者参加,结果将在每个部门显示。
我现在必须做的是修改这一查询,以显示每个部门的雇员总数,除了我已经获得的参加总人数之外,还显示每个部门的雇员总数,然后显示参与百分比,即参加总人数/雇员总数。
" 强 ",如何做到这一点? " /强 "
查询:
SELECT
TOP (100) PERCENT
COUNT(DISTINCT CompleteSurvey.RespondantID) AS [Total Number of Participants],
dbo.Answers.Answer, dbo.Questions.Question,
dbo.Departments.DepartmentName
FROM
dbo.Questions
INNER JOIN
dbo.QuestionsAnswers ON dbo.Questions.QuestionID = dbo.QuestionsAnswers.QuestionID
INNER JOIN
dbo.Answers ON dbo.QuestionsAnswers.AnswerID = dbo.Answers.AnswerID
CROSS JOIN
dbo.Departments
LEFT OUTER JOIN
(SELECT
dbo.Employees.OrgCode, CompleteSurvey_1.QuestionsAnswersID,
CompleteSurvey_1.RespondantID
FROM
dbo.CompleteSurvey AS CompleteSurvey_1
INNER JOIN
dbo.Employees ON dbo.Employees.EmployeeID = CompleteSurvey_1.RespondantID) AS CompleteSurvey ON dbo.QuestionsAnswers.ID = CompleteSurvey.QuestionsAnswersID AND dbo.Departments.OrgCode = CompleteSurvey.OrgCode
GROUP BY
dbo.Answers.Answer, dbo.Questions.Question, dbo.Departments.DepartmentName
ORDER BY
dbo.Questions.Question, dbo.Answers.Answer, dbo.Departments.DepartmentName