Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - SUM of a group of time differences

I want to sum all the time differences to show the total hours worked by a volunteer. Getting a result set of time differences is easy:

Select timediff(timeOut, timeIn) 
FROM volHours 
WHERE username = 'skolcz'

which gives the list of times by hours but then I want to sum it up to a grand total.

So if the result set is:

12:00:00
10:00:00
10:00:00
08:00:00

It would just total 40 hours.

This there a way to do something like:

SELECT SUM(Select timediff(timeOut,timeIn) 
FROM volHours 
WHERE username = 'skolcz') as totalHours

?

like image 704
Wally Kolcz Avatar asked Feb 09 '13 16:02

Wally Kolcz


2 Answers

Select  SEC_TO_TIME(SUM(TIME_TO_SEC(timediff(timeOut, timeIn)))) AS totalhours
FROM volHours 
WHERE username = 'skolcz'

If not then maybe:

Select  SEC_TO_TIME(SELECT SUM(TIME_TO_SEC(timediff(timeOut, timeIn))) 
FROM volHours 
WHERE username = 'skolcz') as totalhours
like image 116
Matthew Avatar answered Oct 20 '22 00:10

Matthew


You almost get the answer from Matthew, all you need to do is to add cast :

Select CAST(SUM(timediff(timeOut, timeIn)) as time) as totalhours
FROM volHours 
WHERE username = 'skolcz'    
like image 41
a1ex07 Avatar answered Oct 19 '22 23:10

a1ex07