我有一个名为Employee
的sql表,其中包含EmployeeID,Status和DepartmentID列。现在我被分配了创建表格报告的任务,其中我需要一个部门的员工总数,活跃的员工数量(状态)和部门ID。
使用Group By子句获取总员工数和相应的DepartmentID很简单。
Select count(*) as 'Total Employees',DepartmentID from Employees
Group By DepartmentID
如何获得具有与外部查询相同的DepartmentID的特定状态的员工计数? 如果您想要更多地澄清问题,请告诉我。
答案 0 :(得分:1)
如果列Status
的值有效,则无需外部查询
Select count(*) as 'Total Employees',
SUM(CASE WHEN status='Active' THEN 1 ELSE 0 END ) as TotalActiveEmployees,
DepartmentID
from Employees
Group By DepartmentID
答案 1 :(得分:0)
试试这个。
Select count(*) as 'Total Employees',
COUNT(CASE WHEN status='Active' THEN 1 ELSE 0 END ) as TotalActiveEmployees,
DepartmentID
from Employees
Group By DepartmentID