We have a SQL 2008 table that holds the attendance data in the following sequence.
uid clock collectdate type 1 2012-10-30 16:13:07.000 2012-10-30 19:29:58.000 in 1 2012-10-30 21:18:50.000 2012-10-30 22:29:58.000 in 2 2012-10-30 16:13:07.000 2012-10-30 19:29:58.000 in 2 2012-10-30 21:18:50.000 2012-10-30 22:29:58.000 in
The proper sequence will be to have the “Type” column as, in and out. But this is exactly the problem I am facing as the system is being use to manage contractors and part timers who always miss pressing the correct button or not at all on the timeclock before making an in or out punch.
We currently depend on this stored procedure to put the timeclock data in the proper order
INSERT INTO attendanceTBL (UID,CollectDate,Clockin,Clockout)
SELECT
UID
,CollectDate
,MAX(case when type = 'in' then clock end) clockin
,MAX(case when type = 'Out' then clock end) clockout
From RAWCLOCKTBL
GROUP BY UID,CollectDate
RETURN
Is there any way I can move the “clock” value to the same row in the correct sequence while ignoring the “type” column. ie, move the above table to.
uid clock clock 1 2012-10-30 16:13:07.000 2012-10-30 21:18:50.000 2 2012-10-30 16:13:07.000 2012-10-30 21:18:50.000
The following query groups the IN and OUT times of a UID for each date and JOINs on the UID and DATE to get the desired output. The LEFT JOIN will ensure to return those entries without an OUT yet (if that is needed).