2014-02-26 39 views
0

我有每天記錄所有時間的考勤表,並且我應該做一個報告以顯示連續n天沒有打卡的所有員工連續n天獲得缺席員工名單

表稱爲TimeAttendance(ID,員工,AttendanceDate,檢入,檢出)

+0

且存在他打了個這一天地位或不 – user3354807

回答

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 

希望這將幫助你

問候

+0

查詢現在 – user3354807

+0

是它的工作原理返回了一個錯誤,謝謝你的回覆快 – user3354807