开发者

MySQL adding the difference between time values to find the avg difference.

I have a column that is Time formatted it needs to be sorted newest to oldest. What I would like to do is find the differences in time between each adjoin record. The tricky part is I need to开发者_如何学C sum all of the time differences then divide by the count – 1 of all the time records. Can this be done in MySQL


Im sorry if i am being a bit too wordy, but i cant quite glean your level of mysql experience.

Also apologies if i dont understand your question. But here goes...

First of all, you dont need to sum and devide, MySQL has an average function for you called avg(). See here for details http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

What you want can be done by sub-queries i think. For more info on subqueries look here. http://dev.mysql.com/doc/refman/5.0/en/select.html

Basically, you want to first create a table that sorts the column.

 SELECT someid, time
    FROM table
    ORDER BY TIME

Use that in a subquery that joins the table with itself but with a shifted index (To get the time before and time after)

SELECT *
    FROM table1 as t1 INNER JOIN table2 as t2 ON t1.someid = t2.someid+1

And use avg on that

SELECT avg(t2.time-t1.time)
GROUP BY t1.someid
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜