天天看点

mysql 每小时 平均_mysql-SQL:每小时平均间隔几天

如果要多天,则需要GROUP BY子句中的日期和小时.目前,它是对几天内同一小时的所有值进行平均. (今天的11,昨天的11等的值相加得出平均值)

我没有一种简单的方法来测试它,但是类似:

SELECT

TRUNCATE(AVG(TIME_TO_SEC(TIMEDIFF(END_DATE, START_DATE))/60), 2) as elapsed,

EXTRACT(DAY FROM END_DATE) as day,

EXTRACT(HOUR FROM END_DATE) as hour

FROM

TIME_INFO

WHERE

EXTRACT(HOUR FROM END_DATE) BETWEEN 9 AND 18 AND

DATE(END_DATE) > CURDATE() - INTERVAL 3 DAY

GROUP BY

EXTRACT(DAY FROM END_DATE),

EXTRACT(HOUR FROM END_DATE)

ORDER BY

END_DATE DESC

如果您希望它跨月/年边界运行,则可能需要DATE(END_DATE)而不是EXTRACT(DAY FROM END_DATE),在这种情况下,输出可能类似于:

elapsed date hour

-------------------------

2.5 2011/03/25 11

1.7 2011/03/25 10

2.4 2011/03/25 9

1.9 2011/03/24 18

2.4 2011/03/24 17

4.0 2011/03/24 16