Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avg Time difference in mysql

Tags:

mysql

Is there a function to find average time difference in the standard time format in my sql.

like image 766
rupa Avatar asked Aug 05 '09 04:08

rupa


People also ask

How do you find the average of time in SQL?

To calculate this average, you can use AVG command. – Averages per minute: to obtain the averages per minute, you must retrieve E3TimeStamp's seconds and milliseconds. To do so, multiply this field by 24 (to convert the time base into hours), and then by 60 (to convert it into minutes).

How do I get AVG in MySQL?

MySQL AVG function is used to find out the average of a field in various records. You can take average of various records set using GROUP BY clause. Following example will take average all the records related to a single person and you will have average typed pages by every person.

How can I find the difference between two dates and minutes in MySQL?

Discussion: To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR .


1 Answers

You can use timestampdiff to find the difference between two times.

I'm not sure what you mean by "average," though. Average across the table? Average across a row?

If it's the table or a subset of rows:

select
    avg(timestampdiff(SECOND, startTimestamp, endTimestamp)) as avgdiff
from
    table

The avg function works like any other aggregate function, and will respond to group by. For example:

select
    col1,
    avg(timestampdiff(SECOND, startTimestamp, endTimestamp)) as avgdiff
from
    table
group by col1

That will give you the average differences for each distinct value of col1.

Hopefully this gets you pointed in the right direction!

like image 121
Eric Avatar answered Sep 25 '22 20:09

Eric