想要在与其他数据相关的两个日期中进行迭代

Want to make iteration in two dates related to other data

我在 sql 服务器数据库中有一个视图,其中包含员工的出勤情况以及这样存储的数据

------------------------------------------------------------------
ID   | EmpID |  Name   |  ArrivalTime | LeavingTime | DayDate
------------------------------------------------------------------
1    |  5    | Hassan  | 9.00AM       | 5.50PM      | 19-11-2010
------------------------------------------------------------------
1    |  5    | Hassan  | 8.50AM       | 5.00PM      | 20-11-2010
------------------------------------------------------------------
1    |  5    | Hassan  | 8.30AM       | 4.54PM      | 23-11-2010
------------------------------------------------------------------

。他在这家公司的开始日期作为 StartDate 列存储在 main EmployeeTable 中,他的结束日期也存储在 我想根据上面的数据获取该员工的缺勤天数,如果他有 EndDate,它必须以存储在 EmployeeTable 中的 StartDate 开始,并以存储的 EndDate 结束。 例如,这位员工于 20-10-2010 开始与我们一起工作 他出席的第一天 table 是 5-11-2010 我希望所有这些日子都在视图中日复一日地提到

------------------------------------------------------------------
ID   | EmpID |  Name   | AbsenceDay
------------------------------------------------------------------
1    |  5    | Hassan  | 21-10-2010
------------------------------------------------------------------
1    |  5    | Hassan  | 22-10-2010
------------------------------------------------------------------
1    |  5    | Hassan  | 23-10-2010
------------------------------------------------------------------

等等 还有

------------------------------------------------------------------
ID   | EmpID |  Name   | AbsenceDay
------------------------------------------------------------------
1    |  5    | Hassan  | 21-11-2010
------------------------------------------------------------------
1    |  5    | Hassan  | 22-11-2010
------------------------------------------------------------------

AttendanceTable 中的缺勤日 提前致谢。

我了解您想要员工在其受雇期间未参加的所有日期。

一种选择是使用递归查询生成工作天数列表,然后使用 not exists 进行过滤。

with cte as (
    select empID, name, startDate, endDate from employees
    union all
    select empID, name, dateadd(day, 1, startDate), endDate 
    from cte 
    where startDate < endDate
)
select empID, name, startDate absenceDay
from cte c
where not exists (
    select 1 from attendances a where a.empID = c.empID and a.dayDate = c.startDate
)

如果雇佣期超过 3 个月,您可以在查询的最后添加 option (maxrecursion 0)