我需要计算 SQL 中在某个日期范围内连续缺勤 X 次的所有员工。
我们有一个缺勤表,其中包含员工每天缺勤的 1 条记录,还有一个日历表,其中包含一年中的工作日。
tblAbsences
EmployeeID int
AbsenceDate datetime
tblCalendar
WorkDay datetime
有谁知道如何计算连续缺勤吗?示例:2009 年 1 月 1 日至 2009 年 3 月 1 日期间连续缺勤 3 次的所有员工。
这应该对你有用。在 ConsecDates 上进行 GROUP BY 查找谁缺席次数超过 X 次。
select a.*,
(
select min(b.absenceDate) from tblAbsences b where a.employeeId = b.employeeId
and b.absenceDate >= a.absenceDate
and not exists (
select 1 from tblabsences c where c.employeeId = b.employeeId and dateadd( dd, 1, b.absenceDate) = c.absenceDate
)
) ConsecDates
from dbo.tblAbsences a
order by a.AbsenceDate asc
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)