我有以下数据库设计:
Employees Table: EmployeeID, Name, OrgCode
Departments Table: OrgCode, DepatName
CompleteSurvey Table: ID, ParticipantID
我需要开发一个表格,显示每个部门的员工总数,以及每个部门完成调查的参与者总数。然后,我想显示每个部门的参与百分比,主要等于参与者总数/员工总数。
我想出了以下两个查询,但我必须提出一个显示上述要求的查询,那么该怎么做?
每个部门的员工总数:
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
每个部门的参与者总数:
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
显示参与者的第二个查询的问题,即使他们没有参与者也没有显示所有部门,它应该显示零。
更新
基本上,我想要的是有一个查询,显示每个部门的员工总数以及每个部门完成调查的参与者总数,并显示参与的百分比。
答案 0 :(得分:2)
你看起来像这样(使用表变量)
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
输出:
Total Number of Employees DepartmentName
2 dept 1000
2 dept 2000
0 no employee dept 3000
最后我认为你要求的是
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
答案 1 :(得分:2)
如果我正确理解,当员工提交调查时,CompleteSurvey会填充EmployeeID,此查询将检索您需要的信息。您可以查看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
如果您需要所有部门,请使用此查询:
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