连续n天获得缺席员工名单

时间:2014-02-26 09:05:25

标签: sql

我的桌子记录了每日基准的所有出勤时间,我应该做一份报告,以显示连续n天没有打入的所有员工

表名为TimeAttendance(Id,Employee,AttendanceDate,CheckIn,CheckOut)

1 个答案:

答案 0 :(得分:0)

根据您在上面添加的要求,在这里可以做什么

select Employee,FromDate=Min(AbsentDate),ToDate=Max(AbsentDate)
from(
select Employee,AbsentDate,count(*) over(partition by Employee,val) as AbsentDays,
       dense_rank() over(partition by Employee order by val) as OrderId
from
(select Employee,AttendanceDate as AbsnetDate,dateadd(d,-row_number() over(partition by Employee order by AttendanceDate),AttendanceDate) as val
from TimeAttendance 
where Status='Absent' -- assuming that if the employee didn't punch the Status will be 'Absent') t) x
where AbsentDays= n -- n is the n consecutive days
group by Employee

- 这里是修正

select Employee,FromDate=Min(AbsentDate),ToDate=Max(AbsentDate)
from(
    select Employee,AbsentDate,count(*) over(partition by Employee,val) as AbsentDays,
           dense_rank() over(partition by Employee order by val) as OrderId
    from
        (select Employee,AttendanceDate as AbsentDate,dateadd(d,-row_number() over(partition by Employee order by AttendanceDate),AttendanceDate) as val
        from TimeAttendance 
        where Status='Absent') as t -- assuming that if the employee didn't punch the Status will be 'Absent') t) x
)as x
where AbsentDays= n -- n is the n consecutive days
group by Employee

希望这会对你有所帮助

问候