如何生成缺席日期的缺席报告

时间:2014-10-22 15:20:48

标签: sql sql-server

我要求生成缺席报告。员工被分配到特定的日期范围,每日冲压IN / OUT记录到CheckINOUT表。

CheckINOUT表
雇员 - 诠释
CheckINOUT-SMALLDATETIME

EmpAllocation表
雇员 - 诠释
起始日期,SMALLDATETIME
EndDate-smalldatetime

报告格式
EmployeeID |缺席日期

TIA。

3 个答案:

答案 0 :(得分:1)

我不确定这是否符合您的实际要求,但这可能是您的一个开始。试试这个(未经测试)。

SELECT * 
FROM   empallocation A 
WHERE  NOT EXISTS (SELECT 1 
                   FROM   checkinout B 
                   WHERE  A.employeeid = B.employeeid 
                          AND b.checkinout BETWEEN a.startdate AND b.enddate) 

我根据您提供的链接添加此代码。

SQL FIDDLE DEMO

CREATE TABLE empalloc 
  ( 
     eid       INT, 
     startdate DATETIME, 
     endate    DATETIME 
  ) 

INSERT INTO empalloc 
VALUES     (001,'2014-10-01','2014-10-15'), 
           (002,'2014-10-10','2014-10-15') 

CREATE TABLE checkinout 
  ( eid     INT, 
    checkin DATETIME ) 

INSERT INTO checkinout 
VALUES     (001,'2014-10-03'),(001,'2014-10-04'),(001,'2014-10-08'),
           (001,'2014-10-09'),(001,'2014-10-10'),(001,'2014-10-11'),
           (001,'2014-10-13'),(001,'2014-10-15'),(002,'2014-10-12'),
           (002,'2014-10-13') 

;WITH cte 
     AS (SELECT eid, 
                startdate, 
                endate 
         FROM   empalloc 
         UNION ALL 
         SELECT eid, 
                Dateadd(dd, 1, startdate) startdate, 
                endate 
         FROM   cte 
         WHERE  startdate < endate) SELECT eid, 
       startdate 
FROM   cte 
WHERE  startdate >= '2014-10-01' 
       AND startdate < '2014-10-10' 
EXCEPT 
SELECT eid, 
       checkin 
FROM   checkinout 

在where条件中,只需添加需要生成报告的日期范围

答案 1 :(得分:0)

use `LEFT JOIN`

SELECT EA.EmployeeID , EA
FROM EmpAllocation EA
LEFT JOIN CheckINOUT C
on EA.EmployeeId = C.EmployeeID
AND C.CheckINOUT between EA.StartDate and EA.EndDate
WHERE C.checkINOUT is NULL

答案 2 :(得分:0)

这将列出每位员工的所有缺席。

;WITH DateRange AS
(
  SELECT EmployeeID, StartDate, EndDate FROM @EmpAllocation
  UNION ALL
  SELECT  EmployeeID, StartDate + 1, EndDate FROM DateRange WHERE StartDate < EndDate
)


select 
    d.EmployeeID, d.StartDate
from DateRange d
    left join @CheckINOUT c on c.EmployeeID = d.EmployeeID and c.CheckInOut = d.StartDate
where c.CheckInOut is null
order by d.EmployeeID, d.StartDate;