I need to calculate all Employees that have X number of consecutive absences within a date range in SQL.
We have an Absences Table with 1 record for each day an emp
This should work for you. GROUP BY on ConsecDates to find who was absent more than X number of times.
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