I have a table that stores clock entries in the following order.
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
My current solution is the use of Min/Max date with case and left join to arrange the entries in the following order
UID Date ClockIn ClockOut
=== ==== ======= ========
R01 2013-01-01 17:00:00 23:45:00
How should I handle the the situation when the clock entry span past midnight. ie,
UID Clock Status
=== ===== ======
R01 2013-01-01 17:00:00 Clockin
R01 2013-01-02 00:45:00 Clockin
As the above will produce 2 entries like below which will result in zero time instate of 7.45 hours for the employee.
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
Thanks in advance.
Here's the code to the stored procedure use in 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