我有一个按以下顺序存储时钟条目的表。
UID Clock Status
=== ===== ======
R01 2013-01-01 17:00:00 Clockin
R01 2013-01-01 17:10:00 Clockin
R01 2013-01-01 23:45:00 Clockin
我目前的解决方案是使用 Min/Max date with case and left join 按以下顺序排列条目
UID Date ClockIn ClockOut
=== ==== ======= ========
R01 2013-01-01 17:00:00 23:45:00
当时钟输入跨越午夜时,我应该如何处理这种情况。IE,
UID Clock Status
=== ===== ======
R01 2013-01-01 17:00:00 Clockin
R01 2013-01-02 00:45:00 Clockin
由于上述将产生 2 个如下条目,这将导致员工的零时间状态为 7.45 小时。
UID Date ClockIn ClockOut
=== ==== ======= ========
R01 2013-01-01 2013-01-01 17:00:00 2013-01-01 17:00:00
R01 2013-01-02 2013-01-02 00:45:00 2013-01-02 00:45:00
提前致谢。
这是 SQL 2008R2 中使用的存储过程的代码
SELECT A.Device,A.DID, A.Name, A.ClockDate,Clockin ,ClockOut
FROM
(
SELECT Device,DID,Name, CONVERT(DATE, DeviceClock) 'ClockDate',
min(case when clock=Clock and Status ='Clock In' OR status='Clock Out' OR status='Access In' then clock end) 'CLock In'
FROM TABLE
group by Device,DID,Name, CONVERT(DATE, DeviceClock)
) as A
LEFT JOIN
(
SELECT Device,DID,Name, CONVERT(DATE, DeviceClock) 'ClockDate',
max(case when clock=Clock and Status ='Clock in' or status='Clock Out' OR status='Access In' then Clock end) 'Clock Out'
FROM TABLE
group by Device,DID,Name, CONVERT(DATE, DeviceClock)
) as B
ON A.DID = B.DID AND A.ClockDate = B.ClockDate