Sql 获取连续n天缺勤员工名单
我有一张记录每天所有考勤的表格,我应该做一份报告,显示所有连续n天未上班的员工Sql 获取连续n天缺勤员工名单,sql,Sql,我有一张记录每天所有考勤的表格,我应该做一份报告,显示所有连续n天未上班的员工 名为TimeAttention(Id、员工、AttendanceDate、CheckIn、CheckOut)的表根据您上面添加的要求,这里是您可以做的事情 select Employee,FromDate=Min(AbsentDate),ToDate=Max(AbsentDate) from( select Employee,AbsentDate,count(*) over(partition by Employee
名为TimeAttention(Id、员工、AttendanceDate、CheckIn、CheckOut)的表根据您上面添加的要求,这里是您可以做的事情
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
希望这能对你有所帮助
关于,他今天打了还是没有打