MySQL - SUM of a group of time differences

送分小仙女□ 提交于 2019-11-30 04:44:20

问题


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

?


回答1:


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



回答2:


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'    



回答3:


try something like that

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


来源:https://stackoverflow.com/questions/14790098/mysql-sum-of-a-group-of-time-differences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!