【问题标题】:Calculating Consecutive Absences in SQL在 SQL 中计算连续缺勤
【发布时间】:2025-11-27 10:25:01
【问题描述】:

我需要在 SQL 中计算一个日期范围内连续缺勤 X 次的所有员工。

我们有一个缺勤表,其中包含员工缺勤每一天的 1 条记录,以及一个包含该年工作日的日历表。

tblAbsences
EmployeeID int
AbsenceDate datetime

tblCalendar
WorkDay datetime

有人知道如何计算连续缺勤吗?示例:在 2009 年 1 月 1 日至 2009 年 3 月 1 日期间连续缺勤 3 次的所有员工。

【问题讨论】:

标签: sql sql-server tsql


【解决方案1】:

这应该适合你。在 ConsecDates 上进行 GROUP BY 查找谁缺席超过 X 次。

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

【讨论】:

  • 这很好用,除了我还需要排除周末。示例:周四、周五和周一连续 3 天休息。 (周六和周日不会出现在 tblAbsences 中)
  • 如果 b.AbsenceDate 是星期五,我添加了 3 天,并且它似乎正在工作。谢谢! ... AND (DATEADD(dd, 1, b.absenceDate) = c.absenceDate OR ((DATENAME(dw, b.absenceDate) = 'Friday') AND DATEADD(dd, 3, b.absenceDate) = c.absenceDate )
  • 假期呢?此解决方案根本不使用您的日历表。
  • Holidays 是一个不错的选择。如果有人能把它加进去,那就太好了。
【解决方案2】:

在 PostgreSQL 中测试; SQL 使用来自发布的示例值。

在提供的表上没有定义主键,下面的代码解决了这个问题。最好添加主键并优化以下代码以利用它们:更好的数据质量、更好的性能、更简洁的代码、更快乐的人。

删除 tbl 前缀为数据库实现提供了更大的灵活性。然后可以互换使用表、视图和同义词,而不会影响引用数据库对象的代码或破坏命名约定。

/* period length as the desired number of consecutive days */
/* time window as the period to be analyzed */
SELECT DISTINCT
 /* Consolidate employees with multiple periods */
 a.employeeid
FROM
 (SELECT
   /* Generate all possible periods */
   pk_c.workday begin_date,
   /* End date for given period length; less one for closed boundaries */
   LEAD(pk_c.workday,3-1,NULL) OVER (ORDER BY pk_c.workday) end_date 
  FROM (SELECT DISTINCT
         /* No calendar PK, remove dupes; if PK, pull in-line view up */
         c.workday
        FROM sandbox.calendar c) pk_c
       ) p
 INNER JOIN sandbox.absences a ON 
  /* Match absences with periods */
  (a.absencedate BETWEEN p.begin_date AND p.end_date)
WHERE
 /* In time window desired; exclude periods extending beyond boundaries */
 (p.begin_date BETWEEN '2009-01-01' AND '2009-03-01'
  AND /* NOT NULL exclusion implied for periods beyond calendar boundaries */
  p.end_date BETWEEN '2009-01-01' AND '2009-03-01')
GROUP BY
 a.employeeid,
 /* Also group period, display only employee */
 p.begin_date
HAVING
 /* Number of absence days to match to the period length */
 /* DISTINCT due to missing absences PK; if PK, drop DISTINCT */
 COUNT(DISTINCT a.absencedate) = 3
;

享受。剥离版本如下:

SELECT DISTINCT
 a.employeeid
FROM
 (SELECT
   pk_c.workday begin_date,
   LEAD(pk_c.workday,3-1,NULL) OVER (ORDER BY pk_c.workday) end_date 
  FROM (SELECT DISTINCT c.workday FROM sandbox.calendar c) pk_c) p
 INNER JOIN sandbox.absences a ON 
  (a.absencedate BETWEEN p.begin_date AND p.end_date)
WHERE
 (p.begin_date BETWEEN '2009-01-01' AND '2009-03-01'
  AND p.end_date BETWEEN '2009-01-01' AND '2009-03-01')
GROUP BY
 a.employeeid, p.begin_date
HAVING
 COUNT(DISTINCT a.absencedate) = 3
;

【讨论】: