Solved

Sum of Times

Posted on 2004-04-16
2
925 Views
Last Modified: 2008-02-26
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
Comment
Question by:Edeldragon
2 Comments
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 10844255
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
 
LVL 17

Accepted Solution

by:
leannonn earned 50 total points
ID: 10844318
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Creating and Managing Databases with phpMyAdmin in cPanel.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now