简体   繁体   English

如何在 MySQL 中生成一系列每小时平均值?

[英]How do I generate a series of hourly averages in MySQL?

I've got data in ten minutes intervals in my table:我的表中有十分钟间隔的数据:

2009-01-26 00:00:00      12
2009-01-26 00:10:00      1.1
2009-01-26 00:20:00      11
2009-01-26 00:30:00      0
2009-01-26 00:40:00      5
2009-01-26 00:50:00      3.4
2009-01-26 01:00:00      7
2009-01-26 01:10:00      7
2009-01-26 01:20:00      7.2
2009-01-26 01:30:00      3
2009-01-26 01:40:00      25
2009-01-26 01:50:00      4
2009-01-26 02:00:00      3
2009-01-26 02:10:00      4
etc.

Is it possible to formulate a single SQL-query for MySQL which will return a series of averages over each hour?是否可以为 MySQL 制定一个单一的 SQL 查询,它会返回每小时的一系列平均值?

In this case it should return:在这种情况下,它应该返回:

5.42
8.87
etc.

It's unclear whether you want the average to be aggregated over days or not.目前尚不清楚您是否希望在几天内汇总平均值。

If you want a different average for midnight on the 26th vs midnight on the 27th, then modify Mabwi's query thus:如果您想要 26 日午夜与 27 日午夜的平均值不同,则修改 Mabwi 的查询:

SELECT AVG( value ) , thetime
FROM hourly_averages
GROUP BY DATE( thetime ), HOUR( thetime )

Note the additional DATE() in the GROUP BY clause.请注意GROUP BY子句中的附加DATE() Without this, the query would average together all of the data from 00:00 to 00:59 without regard to the date on which it happened.如果没有这个,查询将从00:0000:59所有数据一起平均,而不考虑它发生的日期。

This should work:这应该有效:

SELECT AVG( value ) , thetime
FROM hourly_averages
GROUP BY HOUR( thetime )

Here's the result这是结果

AVG(value)          thetime
5.4166666865349     2009-01-26 00:00:00
8.8666666348775     2009-01-26 01:00:00
3.5                 2009-01-26 02:00:00

There is also another possibility considering the fact that dates have a string representation in the database:考虑到日期在数据库中具有字符串表示形式,还有另一种可能性:

You can use SUBSTRING(thetime, 1, [len]) , extracting the common part of your group.您可以使用SUBSTRING(thetime, 1, [len]) ,提取您组的公共部分。 For the example with hourly averages you have the SQL query对于每小时平均值的示例,您有 SQL 查询

SELECT SUBSTRING(thetime, 1, 13) AS hours, AVG(value) FROM hourly_averages GROUP BY hours

By the len parameter you can specify the aggregated time interval considering the MySQL date format yyyy-MM-dd HH:mm:ss[.SS...] :通过len参数,您可以指定考虑到 MySQL 日期格式yyyy-MM-dd HH:mm:ss[.SS...]的聚合时间间隔:

  • len = 4 : group by years len = 4 : 按年分组
  • len = 7 : group by months len = 7 : len = 7分组
  • len = 10 : group by days len = 10 : 按天分组
  • len = 13 : group by hours len = 13 : 按小时分组
  • len = 16 : group by minutes len = 16 : 按分钟分组
  • len = 19 : group by seconds len = 19 : 按秒分组

We encountered a better performance of this method over using date and time function, especially when used in JOINs in MySQL 5.7.我们发现这种方法的性能优于使用日期和时间函数,尤其是在 MySQL 5.7 中的 JOIN 中使用时。 However in MySQL 8 at least for grouping both ways seem to take approximately the same time.然而,在 MySQL 8 中,至少对两种方式进行分组似乎需要大约相同的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM