I have the following database design:
Employees Table: EmployeeID, Name, OrgCode
Departments Table: OrgCode, DepatName
CompleteSurvey Table: ID, ParticipantID
And I need to develop a table that shows the total number of employees in each department, and the total number of participants who completed the survey in each department, too. Then, I want to show the percentage of participation in each department which is mainly equal to the total number of participants / total number of employees.
I came up with the following two queries but I have to come up with one query that shows the above requirement, so how to do that?
Total number of employees in each department:
SELECT COUNT(DISTINCT dbo.Employees.EmployeeID) AS [Total Number of Employees],
dbo.Departments.DepartmentName
FROM dbo.Departments
INNER JOIN dbo.Employees ON dbo.Departments.OrgCode = dbo.Employees.OrgCode
GROUP BY dbo.Departments.DepartmentName
Total number of participants in each department:
SELECT COUNT(DISTINCT dbo.Employees.EmployeeID) AS [Total Number of Employees],
dbo.Departments.DepartmentName
FROM dbo.Departments
INNER JOIN dbo.Employees ON dbo.Departments.OrgCode = dbo.Employees.OrgCode
INNER JOIN dbo.CompleteSurvey ON dbo.Employees.EmployeeID = dbo.CompleteSurvey.RespondantID
GROUP BY dbo.Departments.DepartmentName
The problem with the second query that shows the participants, it doesn’t show all the departments even if they have no participants, it should show zeros.
UPDATE:
Basically, what I want is to have one query that shows the total number of employees in each department and the total number of participants who completed the survey in each department, and show the percentage of participation.
If I understood correctly that CompleteSurvey is filled with EmployeeID when employe submits a survey, this query will retrieve the info you need. You can check live test @ Sql Fiddle.
If you need all the departments, use this query:
New test @ Sql Fiddle.