我正在努力了解员工迟到的频率
到目前为止我已经使用了这个
SELECT EMPID
,EMPNAME
,COUNT(TimeIn) AS LATECOUNT FROM
(SELECT EMPLOYEEID AS EMPID, EmployeeName AS EMPNAME, AbsenceDate AS ABSDAT, MIN(TimeIn) AS TimeIn
FROM dbo.viewAttendance
GROUP BY AbsenceDate, EMPLOYEEID, EmployeeName
HAVING (MIN(TimeIn) > '08:00:00')
) AS a
使用该代码,我只能检索只有员工迟到至少一行(仅返回30行)的行。我真正想要的是300行(所有员工)的完整回报,显示他们迟到的人数。我的想法不够明确,无法找到正确的语法。我知道问题是在HAVING(MIN(timeIn)> '08:00:00'有人可以帮助我吗?在此先感谢... :)
答案 0 :(得分:1)
SELECT
E.EMPNAME
ISNULL(SUM(CASE WHEN A.TIMEIN > '08:00:00' THEN 1 ELSE 0 END), 0) AS LATE_ATTENDANCE
FROM
dbo.MASTEREMPLOYEE E
LEFT JOIN dbo.viewATTENDANCE A ON A.EMPLOYEEID = E.EMPLOYEEID
答案 1 :(得分:0)
为什么不使用这样的东西:
select e.empname, (select count(1) from dbo.viewAttendance a where a.EmployeeId = e.employeeId and a.timein > '08:00:00') as latecount
from employees e