您基本上希望从员工表中选择每个员工,然后排除在特定日期登录的任何人。简而言之,这会创建一个在给定日期未见过的 PersonID 列表。
此解决方案有效,并附有示例案例:
CREATE TABLE #temp (TimeSheetLogId INT IDENTITY(1,1), InTimeStamp DATETIME, OutTimeStamp DATETIME, PersonID INT);
-- last week
-- PersonID=1, didnt come to work last monday
-- INSERT #temp VALUES ('2013-11-19 09:00:00.000','2013-11-19 17:00:00.000',1);
INSERT #temp VALUES ('2013-11-20 09:00:00.000','2013-11-20 17:00:00.000',1);
INSERT #temp VALUES ('2013-11-21 09:00:00.000','2013-11-21 17:00:00.000',1);
INSERT #temp VALUES ('2013-11-22 09:00:00.000','2013-11-22 17:00:00.000',1);
INSERT #temp VALUES ('2013-11-23 09:00:00.000','2013-11-23 17:00:00.000',1);
-- 9AM-5PM shift
INSERT #temp VALUES ('2013-11-19 09:00:00.000','2013-11-19 17:00:00.000',2);
INSERT #temp VALUES ('2013-11-20 09:00:00.000','2013-11-20 17:00:00.000',2);
INSERT #temp VALUES ('2013-11-21 09:00:00.000','2013-11-21 17:00:00.000',2);
INSERT #temp VALUES ('2013-11-22 09:00:00.000','2013-11-22 17:00:00.000',2);
INSERT #temp VALUES ('2013-11-23 09:00:00.000','2013-11-23 17:00:00.000',2);
-- 11PM-7AM shift
INSERT #temp VALUES ('2013-11-18 23:00:00.000','2013-11-19 07:00:00.000',3);
INSERT #temp VALUES ('2013-11-19 23:00:00.000','2013-11-20 07:00:00.000',3);
INSERT #temp VALUES ('2013-11-20 23:00:00.000','2013-11-21 07:00:00.000',3);
INSERT #temp VALUES ('2013-11-21 23:00:00.000','2013-11-22 07:00:00.000',3);
INSERT #temp VALUES ('2013-11-22 23:00:00.000','2013-11-23 07:00:00.000',3);
-- this week
INSERT #temp VALUES ('2013-11-25 09:00:00.000',NULL,1);
INSERT #temp VALUES ('2013-11-25 09:00:00.000',NULL,2);
INSERT #temp VALUES ('2013-11-24 23:00:00.000',NULL,3);
DECLARE @ExamineDate DATE = '2013-11-19'
-- You want to select DISTINCT PersonID from your main STAFF table that contains all records, I did this for simplicity!
SELECT DISTINCT(PersonID)
FROM #temp A
EXCEPT
-- this query should remain as-is
SELECT PersonID
FROM #temp A
WHERE
CAST(A.InTimeStamp AS DATE) = @ExamineDate
OR
CAST(A.OutTimeStamp AS DATE) = @ExamineDate
;
DROP TABLE #temp;