1

I have some difficulties with the MySQL commands that I want to do.

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.

These are my data.

enter image description here

How do I group them into results with 5 minute intervals?

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:

enter image description here

I want my output to be like:

enter image description here

Matt
  • 14,906
  • 27
  • 99
  • 149

2 Answers2

2

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`
0
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
Matt
  • 14,906
  • 27
  • 99
  • 149