I am trying to build a query that analyzes data in our time tracking system. Every time a user punches in or out, it makes a row recording the punch time. So if you punch in at 9:00 and punch out at 5:00 there are two rows with those date stamps recorded accordingly. I need a query that will iterate over the rows at basically sum the datediff between workingpunch_ts (the timestamp column) in hours.
Each row does have an identifier that signifies if the punch is a punch in, or punch out (inout_id, 1 for in, 2 for out).
So for example if you had
ID | workingpunch_ts | inout_id
----------------------------------------------
123 | 2011-02-16 09:00:00.000 | 1
124 | 2011-02-16 17:00:00.000 | 2
That would yield a 8 hours. Now I just need to repeat that process for every pair of rows in the table.
Thoughts on how to accomplish this?
In hours, sure
As long as the in and outs are paired, you add all the outs and remove all the ins, e.g.
The main code is in the 2nd and 3rd lines
The datediff-datediff works out the minutes from midnight for each
workingpunch_ts, and if it is a punchout, it is made negative using the CASEinout_idstatement.The others are added for real life scenarios where you need to group by employee and day, within a date range.