[英]How to calculate average time difference between lines?
I have a mysql table 'totolog' : 我有一个mysql表'totolog':
id dt data
1 2013-05-01 06:01:01 hi john
2 2013-05-01 06:04:23 hi bob
3 2013-05-01 07:17:36 hi alex
4 2013-05-01 14:49:41 hi all
How can I retrieve the average time difference between lines? 如何获取行之间的平均时间差?
I suspect something like this: 我怀疑是这样的:
SELECT UNKNOW_FUNCTION(dt) FROM totolog ORDER BY dt ASC;
The average time would be time_max - time_min / number_of_records
平均时间为time_max - time_min / number_of_records
SELECT (max(dt) - min(dt)) / count(dt) as avg_dt
FROM totolog
ORDER BY avg_dt ASC;
Given a dataset like this... 给定这样的数据集...
CREATE TABLE totolog
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,dt DATETIME NOT NULL
,data VARCHAR(20) NOT NULL
);
INSERT INTO totolog VALUES
(1 ,'2012-12-01 06:01:01','hi john'),
(2 ,'2013-01-01 06:04:23','hi bob'),
(3 ,'2013-02-01 07:17:36','hi alex'),
(4 ,'2013-03-28 14:49:41','hi all');
I'd imagine you'd want something like this... 我想你会想要这样的东西...
SELECT FROM_UNIXTIME(AVG(UNIX_TIMESTAMP(dt)))x FROM totolog;
+---------------------+
| x |
+---------------------+
| 2013-01-22 20:33:10 |
+---------------------+
1 row in set (0.00 sec)
...or this... ...或这个...
SELECT FROM_UNIXTIME((MAX(UNIX_TIMESTAMP(dt))+MIN(UNIX_TIMESTAMP(dt)))/2)x
FROM totolog;
+---------------------+
| x |
+---------------------+
| 2013-01-28 22:25:21 |
+---------------------+
1 row in set (0.00 sec)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.