Sum of Times

Hi Experts

I've got a query that returns a whole lot of times in H:MM:SS format. I want to sum all of these to find the total of the column. If I try:

select sum(Times) from (select timediff(time2, time1) as Times from my_table) then the value I get is
a) an integer when I was hoping for either a time or a real number of hours and
b) doesn't agree with the total if I try the same thing in Excel (the values differ by 53 hours (I think it's hours) in the examples I've tried, so it doesn't even look like rounding errors.


Who is Participating?
Aleksandar BradarićConnect With a Mentor Software DeveloperCommented:
You should convert time to seconds, sum it, and convert it back to time. This should do the trick:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(your_time_field))) ...
Unless you have some other reason, I don't see why you need a subquery.  It may just be an issue of the format; give the following a shot:

SELECT TIME_FORMAT(SUM(TIMEDIFF(time2, time1)), '%H:%i:%s') FROM my_table;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.