Calculating Consecutive Absences in SQL

后端 未结 2 1202
余生分开走
余生分开走 2021-01-01 07:44

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

2条回答
  •  失恋的感觉
    2021-01-01 08:37

    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
    

提交回复
热议问题