[英]how to get the average and std deviation of daily totals from table in which date values are stored by hour mysql?
I a do have a table that stores a total count of error by hour, by day. 我确实有一个表,该表按小时,按天存储错误总数。
I need to calculate the average of the total number of errors by day, I was trying something like this: 我需要按天计算错误总数的平均值,我正在尝试如下操作:
SELECT
error_code,
AVG(total) as avg,
STDDEV_SAMP(total) as std
FROM
error_monitoring
WHERE
date_sub(curdate(), interval 30 day) <= date
GROUP BY
error_code
But the result of the query is based on the hourly values I have in the table and no the daily totals. 但是查询的结果是基于表中的小时值,而不是每日总计。
The table in database shows as follow: 数据库中的表如下所示:
id | error_code | total | average | standard_deviation | date
1 | W0334 | 2131 | 81 | 163.349 | 2016-12-20 23:00:00
2 | W0096 | 910 | 45 | 132.915 | 2016-12-20 15:00:00
3 | W0334 | 120 | 81 | 163.349 | 2016-12-20 08:00:00
So, how I can calculate this based on daily totals? 那么,如何根据每日总计来计算呢?
If you want the "average by day", you can calculate the total and then divide by the number of days: 如果要“按天平均”,则可以计算总数,然后除以天数:
SELECT error_code, SUM(total) / COUNT(DISTINCT date(date)) as avg
FROM error_monitoring
WHERE date_sub(curdate(), interval 30 day) <= date
GROUP BY error_code
Your code also includes STDDEV_SAMP()
. 您的代码还包括
STDDEV_SAMP()
。 If you also want that calculation, then you would need to use a subquery, aggregating by day. 如果您还想进行该计算,则需要使用按天汇总的子查询。 Assuming that all error codes are available on each day:
假设每天都有所有错误代码:
SELECT error_code, AVG(day_total), STDDEV_SAMP(day_total)
FROM (SELECT error_code, date(date) as dte, SUM(total) as day_total
FROM error_monitoring
WHERE date_sub(curdate(), interval 30 day) <= date
GROUP BY error_code, date(date)
) em
GROUP BY error_code;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.