SQL Server find datediff between different rows -


i trying build query analyzes data in our time tracking system. every time user punches in or out, makes row recording punch time. if punch in @ 9:00 , punch out @ 5:00 there 2 rows date stamps recorded accordingly. need query iterate on rows @ sum datediff between workingpunch_ts (the timestamp column) in hours.

each row have identifier signifies if punch punch in, or punch out (inout_id, 1 in, 2 out).

so example if 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 yield 8 hours. need repeat process every pair of rows in table.

thoughts on how accomplish this?

in hours, sure

select empid, cast(datediff(d,0,workingpunch_ts) datetime),     sum(case when inout_id = 2 1 else -1 end *     datediff(mi, datediff(d,0,workingpunch_ts), workingpunch_ts))/60.0 hours clock workingpunch_ts between '20110201' , '20110228 23:59:59.999' group empid, datediff(d,0,workingpunch_ts) 

as long in , outs paired, add outs , remove ins, e.g.

 - in  (9)  + out (12)  - in  (13:15)  + out (17) 

the main code in 2nd , 3rd lines
datediff-datediff works out minutes midnight each workingpunch_ts, , if punchout, made negative using case inout_id statement.

the others added real life scenarios need group employee , day, within date range.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -