Sum timelog for each day in sql


I have a table with data looking like this:
Sql result
The purpose of the table is to register a log for Employees so you can see the times they've worked.
As you can see each day has a rowID and also a TimeClockCatID. The rowID is the id for the specific day they are registering for and the TimeClockCatID is showing if they checked in (1) or checked out (2) from the system.

Is it possible for me to count and sum how much time an employee has worked a day by comparing the TimeClockCatID = 1 with TimeClockCatID  = 2 for each rowID?

Thanks for help!

Peter NordbergIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
This should help match your requirements.. Hope the columns I've displayed would be sufficient..please feel free to make any changes as required..
create table ur_table( EmployeeId int, cTimestamp datetime, TimeClockTypeId int, TImeclockCatId int, rowId int)
insert into ur_table values
(120, '2018-07-02 09:00', 3, 1, 129)
,(120, '2018-07-02 10:00', 3, 2, 129)
,(120, '2018-07-02 13:00', 3, 1, 129)
,(120, '2018-07-02 16:00', 3, 2, 129)
,(120, '2018-07-03 08:00', 3, 1, 130)
,(120, '2018-07-03 12:00', 3, 2, 130)
,(120, '2018-07-04 08:15', 3, 1, 131)
,(120, '2018-07-04 11:50', 3, 2, 131)

; with cte as (
select  cTimestamp, EmployeeId, rowId, TimeClockTypeId, TimeclockCatId
, row_number() over (partition by employeeId, TimeclockTypeId, rowid order by ctimestamp) rnum
from ur_table)
select t1.EmployeeId, t1.rowid, cast(t1.cTimestamp as date) cdate, SUM(datediff(mi, t1.ctimestamp, t2.ctimestamp) ) Minutes
from cte t1
left join cte t2 on t1.EmployeeId  = t2.EmployeeId and t1.rowId = t2.rowId and t1.rnum = t2.rnum -1
WHERE t1.TImeclockCatId = 1 and t2.TImeclockCatId = 2
GROUP BY t1.EmployeeId, t1.rowid,cast(t1.cTimestamp as date)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter NordbergIT ManagerAuthor Commented:
Works like a charm:) Thanks!
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Welcome, glad to help..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.