[英]Get average day or week values
I have statistical data like this: 我有这样的统计数据:
time val1
1424166578 51
1424166877 55
1424167178 57
1424167477 57
time
is a unix timestamp. time
是Unix时间戳。 There is one record every 5 minutes excluding nights and sundays. 每5分钟记录一次,晚上和周日除外。 This continues over several weeks.
这持续了几个星期。
Now I want to get these values for an average day and an average week. 现在,我想获取平均一天和一周中的这些值。 The result should include values for every 5 minutes like normal but for average past days or weeks.
结果应包括正常情况下每5分钟的值,但过去几天或几周的平均值。
The result should look like this: 结果应如下所示:
time val1
0 43.423
300 46.635
600 51.887
...
So time could be a timestamp with relative time since day or week start. 因此,时间可能是自一天或一周开始以来的相对时间的时间戳。 Perhaps it is better to use
DATETIME
... not sure. 也许最好使用
DATETIME
...不确定。
If I use GROUP BY FROM_UNIXTIME(time, '%Y%m%d')
for example I get one value for the whole day. 例如,如果我使用
GROUP BY FROM_UNIXTIME(time, '%Y%m%d')
,那么我整天都会得到一个值。 But I want all average values for all days. 但是我想要所有天的所有平均值。
You seem to be interested in grouping dates by five minute intervals instead of dates. 您似乎对按五分钟间隔而不是日期将日期分组感兴趣。 This is fairly straightforward:
这很简单:
SELECT
HOUR(FROM_UNIXTIME(time)) AS HH,
(MINUTE(FROM_UNIXTIME(time)) DIV 5) * 5 AS MM,
AVG(val1) AS VAL
FROM your_table
WHERE time > UNIX_TIMESTAMP(CURRENT_TIMESTAMP - INTERVAL 7 DAY)
GROUP BY HH, MM
The following result will explain how date is clamped: 以下结果将说明如何确定日期:
time FROM_UNIXTIME(time) HH MM
1424166578 2015-02-17 14:49:38 14 45
1424166877 2015-02-17 14:54:37 14 50
1424167178 2015-02-17 14:59:38 14 55
1424167477 2015-02-17 15:04:37 15 00
I would approach this as: 我将其作为:
select date(from_unixtime(time)) as day, avg(val)
from table t
group by date(from_unixtime(time))
order by day;
Although you can use the format argument, I think of that more for converting the value to a string than to a date/time. 尽管您可以使用format参数,但我认为将值转换为字符串而不是日期/时间更重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.