Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Average interval between records

Tags:

Assume this table:

id    date ---------------- 1     2010-12-12 2     2010-12-13 3     2010-12-18 4     2010-12-22 5     2010-12-23 

How do I find the average intervals between these dates, using MySQL queries only?

For instance, the calculation on this table will be

  (     ( 2010-12-13 - 2010-12-12 )   + ( 2010-12-18 - 2010-12-13 )   + ( 2010-12-22 - 2010-12-18 )   + ( 2010-12-23 - 2010-12-22 )   ) / 4 ---------------------------------- = ( 1 DAY + 5 DAY + 4 DAY + 1 DAY ) / 4 = 2.75 DAY 
like image 414
HyderA Avatar asked Nov 30 '10 09:11

HyderA


People also ask

How do I calculate average time between dates in SQL?

Answer: To find the average time between two dates, you could try the following: SELECT TO_DATE(date1, 'yyyy/mm/dd') + ((TO_DATE(date2, 'yyyy/mm/dd') - TO_DATE(date1, 'yyyy/mm/dd')) /2 ) FROM dual; This will calculate the elapsed time between date1 and date2. Then it takes half of the elapsed time and adds it to date1.

How do you calculate average 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 you find the average of a count in MySQL?

MySQL AVG() function retrieves the average value of a given expression. If the function does not find a matching row, it returns NULL. Where expr is a given expression. The DISTINCT option can be used to return the average of the distinct values of expr.


2 Answers

Intuitively, what you are asking should be equivalent to the interval between the first and last dates, divided by the number of dates minus 1.

Let me explain more thoroughly. Imagine the dates are points on a line (+ are dates present, - are dates missing, the first date is the 12th, and I changed the last date to Dec 24th for illustration purposes):

++----+---+-+ 

Now, what you really want to do, is evenly space your dates out between these lines, and find how long it is between each of them:

+--+--+--+--+ 

To do that, you simply take the number of days between the last and first days, in this case 24 - 12 = 12, and divide it by the number of intervals you have to space out, in this case 4: 12 / 4 = 3.

With a MySQL query

SELECT DATEDIFF(MAX(dt), MIN(dt)) / (COUNT(dt) - 1) FROM a; 

This works on this table (with your values it returns 2.75):

CREATE TABLE IF NOT EXISTS `a` (   `dt` date NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;  INSERT INTO `a` (`dt`) VALUES ('2010-12-12'), ('2010-12-13'), ('2010-12-18'), ('2010-12-22'), ('2010-12-24'); 
like image 86
Vegard Larsen Avatar answered Sep 20 '22 22:09

Vegard Larsen


If the ids are uniformly incremented without gaps, join the table to itself on id+1:

SELECT d.id, d.date, n.date, datediff(d.date, n.date) FROM dates d JOIN dates n ON(n.id = d.id + 1) 

Then GROUP BY and average as needed.

If the ids are not uniform, do an inner query to assign ordered ids first.

I guess you'll also need to add a subquery to get the total number of rows.

Alternatively

Create an aggregate function that keeps track of the previous date, and a running sum and count. You'll still need to select from a subquery to force the ordering by date (actually, I'm not sure if that's guaranteed in MySQL).

Come to think of it, this is a much better way of doing it.

And Even Simpler

Just noting that Vegard's solution is much better.

like image 36
Dmitri Avatar answered Sep 20 '22 22:09

Dmitri