我正在建立一个考勤模块。
该过程从导入Timesheet员工文件开始。
时间表员工文件包含以下列(红色圆圈显示来自同一员工的时间记录)
传感器列告诉我们是否为入口传感器( ENT )或退出传感器( EXT )
该信息被加载到我的SQL数据库的临时表中。我的员工表有一个RFID_Card列,因此我可以从时间表中导入数据进行匹配。
然后,我必须运行一个存储过程,我可以在其中插入我的" employee_timerecord"每次记录表。该表看起来像:
我的存储过程SQL脚本应该类似于:
INSERT INTO employee_timerecord (employeeid, timestamp,sensortype,timestamp_type)
SELECT
em.employeeid,
tsi.timestamp,
s.sensortype, -- 0 = Entrance, 1 = Exit
--HERE I HAVE TO CHECK IF THE TIME RECORD FOR THAT EMPLOYEE
--IS THE FIRST ENTRANCE IN THE DAY THEN ASSING A 1, IF ITS THE LAST
--EXIT IN THE DAY THEN ASSIGN A 2, etc
FROM timesheet_import_temp tsi
JOIN sensor s ON s.sensorcode = tsi.sensorcode
JOIN employee em ON em.rfidcard = tsi.rfidcard
所以从上面的示例表中,我应该得到:
Employee with RFID 76:23146: the first timestamp in the day was at: 01/04/2014 06:57:40 a. m.
Employee with RFID 76:23146: the last timestamp in the day was at: 01/04/2014 15:04:48 p. m.
有任何线索吗?
答案 0 :(得分:1)
注意:代码未经过测试
-- Helps creating entries blocks
DECLARE @maxHoursBetweenEntries int = 4;
CASE
WHEN (
SELECT MIN(timestamp)
FROM timesheet_import_temp tsi1
WHERE
rfidcard = tsi.rfidcard
AND sensortype = 0
AND ISNULL(DATEDIFF(
hour,
(
SELECT MAX(timestamp)
FROM timesheet_import_temp
WHERE
AND rficard = tsi1.rficard
AND timestamp < tsi1.timestamp
AND sensortype = 1
),
timestamp
), maxHoursBetweenEntries) >= @maxHoursBetweenEntries
) = tsi.timestamp THEN 1
...
END
答案 1 :(得分:0)
您需要为数据中的第3行和第4行创建第一个或最后一个计数的假设。
我现在正在为你制作一个sql小提琴。
T-SQL join to get both mated and non mated start and stop records