问题
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.
回答1:
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.
select Departments.DepatName,
count (Employees.EmployeeID) Employees,
count (CompleteSurvey.ID) Finished,
cast (count (CompleteSurvey.ID) as float) / count (Employees.EmployeeID)
PercentageFinished
from Departments
inner join Employees
on Departments.OrgCode = Employees.OrgCode
left join CompleteSurvey
on Employees.EmployeeID = CompleteSurvey.ParticipantID
group by Departments.DepatName
If you need all the departments, use this query:
select Departments.DepatName,
count (Employees.EmployeeID) Employees,
count (CompleteSurvey.ID) Finished,
case when count (Employees.EmployeeID) <> 0
then cast (count (CompleteSurvey.ID) as float)
/ count (Employees.EmployeeID)
else 0
end PercentageFinished
from Departments
left join Employees
on Departments.OrgCode = Employees.OrgCode
left join CompleteSurvey
on Employees.EmployeeID = CompleteSurvey.ParticipantID
group by Departments.DepatName
New test @ Sql Fiddle.
回答2:
Are you looking something like this (using tables variables)
DECLARE @Employees Table(EmployeeID INT, Name VARCHAR(50), OrgCode VARCHAR(50) )
DECLARE @Departments Table( OrgCode INT, DepartmentName VARCHAR(50) )
DECLARE @CompleteSurvey Table( ID INT, RespondantID INT)
INSERT INTO @Employees VALUES(12,'Emp 12', 1000),(13,'Emp 13', 1000),(112,'Emp 112', 2000), (113,'Emp 114', 2000)
INSERT INTO @Departments VALUES(1000, 'dept 1000'),(2000, 'dept 2000')
,(3000, 'no employee dept 3000')
INSERT INTO @CompleteSurvey VALUES (901,12), (901,112),(902,13), (902,112)
SELECT COUNT(DISTINCT EmployeeID) AS [Total Number of Employees], D.DepartmentName FROM @Departments D
LEFT OUTER JOIN @Employees E
ON D.OrgCode = E.OrgCode GROUP BY D.DepartmentName
SELECT D.DepartmentName,COUNT(DISTINCT E.EmployeeID) AS [Total Number of Employees],
COUNT(DISTINCT S.RespondantID )AS completed_survey ,
CASE WHEN COUNT(DISTINCT E.EmployeeID)>0 THEN COUNT(DISTINCT S.RespondantID )/
CAST(COUNT(DISTINCT E.EmployeeID)AS FLOAT)*100 ELSE 0 END AS particPerc
FROM @Departments D LEFT OUTER JOIN @Employees E ON D.OrgCode = E.OrgCode
LEFT OUTER JOIN @CompleteSurvey S ON E.EmployeeID =S.RespondantID GROUP BY D.DepartmentName
which outputs:
Total Number of Employees DepartmentName
2 dept 1000
2 dept 2000
0 no employee dept 3000
and finally what I think you're asking for
DepartmentName Total Number of Employees completed_survey particPerc
dept 1000 2 2 100
dept 2000 2 1 50
no employee dept 3000 0 0 0
来源:https://stackoverflow.com/questions/10605262/how-to-show-the-participation-percentage-in-each-department