我有一个应用程序,当一个人进入办公室时,他必须进入办公室,当他离开办公室时,他必须退出。那是一个考勤系统。
现在我想使用在特定日期缺席的SQL查询进行检查。
表格如下: -
选择在特定日期缺席的personid
的查询应该是什么?
答案 0 :(得分:0)
您基本上希望从员工表中选择每位员工,然后排除在特定日期登录的所有员工。简而言之,这会创建一个在给定日期未见的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;
答案 1 :(得分:0)
SELECT PERSONID
FROM <PERSON INFORMATION TABLE>
WHERE PERSONID NOT IN(SELECT PERSONID FROM <TIMING INFORMATION TABLE> WHERE INTIMESTAMP=<PARTICULAR DATE>)