简体   繁体   English

MySQL平均每1个月24小时

[英]MySQL Average every 24 hours of 1 month

So I have a table with 100's of thousands of rows like so: 因此,我有一个表格,其中包含100的数千行,如下所示:

+--+----------+-------------------+
|ID|metervalue  |datetime         |
|1   |10        |2016-02-23 01:00 |
|2   |20        |2016-02-23 02:00 |
|3   |5         |2016-02-23 03:00 |
|... |...       |...              |
|x   |x         |2016-02-24 01:00 |
|x   |x         |2016-02-24 02:00 |
|x   |x         |2016-02-24 03:00 |
|... |...       |...              |
|x   |x         |2016-03-01 01:00 |
|x   |x         |2016-03-01 02:00 |
|x   |x         |2016-03-01 03:00 |
+--+----------+-------------------+

So there is some meter reading taken every hour for many, many days. 因此,很多天以来,每小时都会读取一些仪表读数。

Now I need to get data for a graph. 现在,我需要获取图形数据。 Hourly graph data is easy because I can just query the data as it is, but for "monthly" data I need to query all the rows from a duration of a month so that every 24 hours is averaged into a single row. 每小时图形数据很容易,因为我可以按原样查询数据,但是对于“每月”数据,我需要查询一个月时间范围内的所有行,以便将每24小时平均为一行。

So one row out of 30 (assuming month has 30 days) should contain 24 hours averaged into a 1 day. 因此,每30行中的一行(假设一个月有30天)应包含平均24小时(即1天)。

I tried this solution, but it did not work as I would have wanted it to work: Average of data for every 5 minutes in the given times 我尝试了此解决方案,但由于无法正常运行,因此无法正常工作: 给定时间内每5分钟的平均数据

I don't think the GROUP BY in that answer for example works because I don't want to average, say, every fifth hour of every day, but instead I want to average the 24 hours of a day to make up one day. 例如,我认为该答案中的GROUP BY无效,因为我不想每天平均每五小时求平均值,而是想将一天中的24小时求平均值,以弥补一天的时间。

You still will GROUP BY but you do it by what the DATE() function returns which disregards the time part of the data, like so: 您仍然会使用GROUP BY但是您可以通过DATE()函数返回的结果来执行此操作,而忽略数据的时间部分,如下所示:

SELECT DATE(a.datetime), AVG(a.metervalue)
FROM MyInterestingTable a
GROUP BY DATE(a.datetime)

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

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