• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 935
  • Last Modified:

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.

Thanks

ED
0
Edeldragon
Asked:
Edeldragon
1 Solution
 
snoyes_jwCommented:
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;
0
 
Aleksandar Bradarić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))) ...
---
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now