根据 SQL SERVER 2000 中的条件计算行数
Count Rows On a Condition in SQL SERVER 2000
我想从 SQL SERVER 2000
中获取以下格式的数据
Department | TotalPresent | Employee | Officer
XYZ 12 6 6
我正在 运行 查询
SELECT a.department,
Count(emp_id) AS totalpresent,
CASE
WHEN a.type = 'Employee' THEN Count(a.type)
ELSE 0
END AS employee,
CASE
WHEN a.type = 'Officer' THEN Count(a.type)
ELSE 0
END AS officer
FROM attendancelog m
INNER JOIN employeedata a
ON m.emp_id = a.emp_id groupby a.type,
a.department
当我 运行 这个查询时,我得到了这种不正确的数据
Department | TotalPresent | Employee | Officer
XYZ 12 6 0
XYZ 12 0 6
请纠正我似乎是什么问题为什么它返回我两次相同的记录但满足条件
我只想根据类型是员工还是官员来计算行数
从 group by
中删除 type
并将 case
移到 sum()
中:
Select a.department, count(emp_id) as TotalPresent,
sum(case when a.Type = 'Employee' THEN 1 ELSE 0 END) as Employee,
sum(case when a.Type = 'Officer' THEN 1 ELSE 0 END) as Officer
from AttendanceLog m INNER JOIN
EmployeeData a
ON m.emp_id = a.emp_id
GroupBy a.Department;
另外,升级您的 SQL 服务器。 SQL Server 2000 多年来一直不受支持。您应该尝试使用支持的软件。
我想从 SQL SERVER 2000
中获取以下格式的数据Department | TotalPresent | Employee | Officer
XYZ 12 6 6
我正在 运行 查询
SELECT a.department,
Count(emp_id) AS totalpresent,
CASE
WHEN a.type = 'Employee' THEN Count(a.type)
ELSE 0
END AS employee,
CASE
WHEN a.type = 'Officer' THEN Count(a.type)
ELSE 0
END AS officer
FROM attendancelog m
INNER JOIN employeedata a
ON m.emp_id = a.emp_id groupby a.type,
a.department
当我 运行 这个查询时,我得到了这种不正确的数据
Department | TotalPresent | Employee | Officer
XYZ 12 6 0
XYZ 12 0 6
请纠正我似乎是什么问题为什么它返回我两次相同的记录但满足条件
我只想根据类型是员工还是官员来计算行数
从 group by
中删除 type
并将 case
移到 sum()
中:
Select a.department, count(emp_id) as TotalPresent,
sum(case when a.Type = 'Employee' THEN 1 ELSE 0 END) as Employee,
sum(case when a.Type = 'Officer' THEN 1 ELSE 0 END) as Officer
from AttendanceLog m INNER JOIN
EmployeeData a
ON m.emp_id = a.emp_id
GroupBy a.Department;
另外,升级您的 SQL 服务器。 SQL Server 2000 多年来一直不受支持。您应该尝试使用支持的软件。