Solved

Summing time fields to get total time

Posted on 2011-02-17
8
342 Views
Last Modified: 2012-05-11
Using MySQL.

I have a field called 'duration' that has data in the format  '1899-12-31 00:08:55'

I ned to sum up the TIME portion of this field and show th result as HH:MM:SS, what would the sql synax be fo this??
0
Comment
Question by:BrianFord
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:wolfgang_93
ID: 34919840
Is the duration field datetime or varchar?
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34919867
maybe this:

SEC_TO_TIME(
hour(mydt)*60*60 +
minute(mydt)*60 +
second(mtdt)
)

check all date & time functions here

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34919881
something like this:

select
SEC_TO_TIME(sum(
hour(mydt)*60*60 +
minute(mydt)*60 +
second(mtdt)
)
)
from my table where ...
0
 
LVL 4

Accepted Solution

by:
incerc earned 500 total points
ID: 34919889
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIME(duration))))

I explain below:

TIME(duration) = 00:08:55
TIME_TO_SEC('00:08:55') = this converted in seconds = 8*60 + 55
SUM(..) = sum all those seconds
SEC_TO_TIME = convert back into time format hh:mm:ss

More about this: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Closing Comment

by:BrianFord
ID: 34920060
Perfect thanks very much :)
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34920095
but my solution is basically the same one that you posted :(

TIME(mydt) = hour(mydt)*60*60 + minute(mydt)*60 + second(mtdt)
0
 

Author Comment

by:BrianFord
ID: 34920120
My appologies,

I actually mis-read your solution, if there is a way for me to split the points at this stage I'm happy to do so, soryy :(
0
 
LVL 4

Expert Comment

by:incerc
ID: 34920818
I'm sorry HainKurt, I didn't want to steal your solution, I was unaware of it, we just posted at the same time (you faster, it took me more time to explain my solution).
I'd be happy to share my points with you, if possible :)

BrianFord, glad to help! :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This video discusses moving either the default database or any database to a new volume.
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now