I have the following database design:
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
I wrote a query that shows each question with all possible answers and the total number of participants in each question even if there is one of the possible answers without any participant. This results will be shown in each department.
What I have to do now is to modify this query to show the total number of employees in each department besides the total number of participants that I already got, then show the participation percentage which is the total number of participants/ total number of employees.
So how to do that?
The query:
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