Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I have a MySQL table "tempt" with an ID column and a "tTime" column of data type TIME with two rows:
SQL
1, '00:00:45'
2, '00:01:15'

i.e. row 1 holds 45 seconds and row 2 holds 1 minute and 15 seconds.

If I now run the following query
SQL
SELECT AVG(tTime) from tempt

the answer I get is:
SQL
80.0000


What does that represent? And how can I get the answer I am looking for (which is 1 minute, obviously) ..?

[edit]

OK, I'm a bit slow - 80 is the average of 45 and 115... but wtf use is that to anyone? How and why does MySQL turn 00:01:15 into 115?

[edit 2]

ok, so I have to use:
SQL
SELECT AVG( 360*HOUR(tTime) + 60*MINUTE(tTime) + SECOND(tTime)) from tempt

to get the answer ins econds.

Blimey. Thank you MySQL :/
Posted
Updated 23-Oct-15 6:32am
v4

1 solution

Solved it myself :)
Though I'd still like to know how MySQL turns 00:01:15 into 115
 
Share this answer
 
Comments
CHill60 23-Oct-15 12:42pm    
Possibly because AVG is not a date or time function so it's just stripping away the non-numeric? If you convert tTime to seconds first (TIME_TO_SEC), run the AVG against that and convert back to a time, or minutes or whatever, afterwards?
Wombaticus 23-Oct-15 12:54pm    
Thanks - I'd missed that TIME_TO_SEC function. Much better than my clunky solution above!
CHill60 23-Oct-15 19:29pm    
Cool - as long as it's sorted :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900