Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Average on time column?

SELECT avg( duration ) as average FROM login;

The datatype for duration is "time", thus my value is like: 00:00:14, 00:20:23 etc

I execute the query it gives me: 2725.78947368421

What is that? I want in time format, can mysql do the average on time??

like image 739
mysqllearner Avatar asked Feb 07 '10 14:02

mysqllearner


People also ask

How do I find the average of a timestamp in SQL?

create table demo (startDate datetime); insert demo (startDate) values ('2015-04-10 3:46:07'); insert demo (startDate) values ('2015-04-09 3:47:37'); insert demo (startDate) values ('2015-04-08 3:48:07'); insert demo (startDate) values ('2015-04-07 3:43:44'); insert demo (startDate) values ('2015-04-06 3:39:08'); ...

How do I get the average of a column in MySQL?

MySQL AVG() Function The AVG() function returns the average value of an expression.

Is there a time data type for MySQL?

Introduction to MySQL TIME data typeMySQL uses the 'HH:MM:SS' format for querying and displaying a time value that represents a time of day, which is within 24 hours. To represent a time interval between two events, MySQL uses the 'HHH:MM:SS' format, which is larger than 24 hours.

How does MySQL calculate time?

MySQL TIMEDIFF() Function The TIMEDIFF() function returns the difference between two time/datetime expressions. Note: time1 and time2 should be in the same format, and the calculation is time1 - time2.


1 Answers

Try this:

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(`login`))) FROM Table1; 

Test data:

CREATE TABLE `login` (duration TIME NOT NULL); INSERT INTO `login` (duration) VALUES ('00:00:20'), ('00:01:10'), ('00:20:15'), ('00:06:50'); 

Result:

00:07:09 
like image 55
Mark Byers Avatar answered Oct 01 '22 12:10

Mark Byers