[英]Grouping in 5 minute intervals within a time range
I have some difficulties with the MySQL commands that I want to do.我在执行 MySQL 命令时遇到了一些困难。
I need to generate some graphs obtaining a query in MySQL in intervals, for example, of 5 minutes generating an average of a data with the AVG method.我需要生成一些图表,以 MySQL 的间隔获取查询,例如,5 分钟生成一个数据的平均值,使用 AVG 方法。
These are my data.这些是我的数据。
How do I group them into results with 5 minute intervals?如何以 5 分钟的间隔将它们分组到结果中?
I already generated a query:我已经生成了一个查询:
SELECT FROM_UNIXTIME(FLOOR((UNIX_TIMESTAMP(created_at)) DIV 300)*300) AS t, site_id, AVG(response_time) as c
FROM sites_health_metrics
WHERE site_id = 1
AND created_at BETWEEN '2021-08-23 20:40:00' AND '2021-08-23 21:40:00'
GROUP BY t ORDER BY t
where I get the following:我在哪里得到以下信息:
I want my output to be like:我希望我的 output 是这样的:
This is work这是工作
SELECT series.`range` as `range`, COALESCE(dt.average, 0) as average FROM ( SELECT FROM_UNIXTIME(FLOOR((UNIX_TIMESTAMP(created_at)) DIV 300)*300) AS fecha, site_id, AVG(response_time) as average FROM sites_health_metrics WHERE site_id = 1 AND created_at BETWEEN '2021-08-23 20:40:00' AND '2021-08-23 21:40:00' GROUP BY fecha ) dt RIGHT JOIN ( SELECT date_format(date_add('2021-08-23 20:40:00', INTERVAL @num:=@num+300 SECOND), '%Y-%m-%d %H:%i:%s') `range` FROM sites_health_metrics, (select @num:=-300) num LIMIT ) series ON series.`range` = dt.fecha GROUP BY `range`
SELECT UNIX_TIMESTAMP(created_at) DIV 300 t, site_id, AVG(response_time) c
FROM sites_health_metrics a
WHERE site_id = 1
AND created_at BETWEEN '2021-08-23 20:40:00' AND '2021-08-23 21:40:00'
GROUP BY UNIX_TIMESTAMP(created_at) DIV 300, site_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.