I
I
Ivan2016-11-08 07:45:05
SQL Server
Ivan, 2016-11-08 07:45:05

How to add two time intervals?

Good afternoon.
There is a record of working hours. People come in the morning and leave in the evening. and so the whole month. I counted the time between arrival and departure. Now I need to add up this time difference and get how much each employee was in place. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2016-11-08
@sergey-gornostaev

From the income/departure table, use a subquery to select the employee ID and datediff (in hours) of those records whose dates belong to a certain month, and then group them by the employee ID with the main query and perform summing aggregation of hours.

select employee_id, sum(work_hours) as work_hours from (
    select employee_id, datediff(hh, coming_time, leaving_time) as work_hours
    from work_schedule
    where Year(coming_time) = Year(CURRENT_TIMESTAMP) and Month(coming_time) = Month(CURRENT_TIMESTAMP)
) as sq group by employee_id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question