Mysql search with jumps of hours/days/month

Asked

Viewed 20 times

1

I would like to understand how I can do a search in mysql skipping a certain amount of days of each record, for example, show the records from the beginning of a table to the end by skipping from 7 in 7 days, or from 1 in 1 month. I’ve done something similar limited, follow the code below:

SELECT `id`,`cod`,`corrente`,MIN(`data_hora`) as `data_hora` FROM sensores WHERE `cod` LIKE '001' GROUP BY DATE_FORMAT(`data_hora`, '%Y%m%d%H') ORDER BY `sensores`.`id` ASC

This code skips the records every 1 hour, follow other examples:

        Intervalos por minuto = ... GROUP BY DATE_FORMAT(`data_hora`, '%Y%m%d%H%i')
        Intervalos por hora = ... GROUP BY DATE_FORMAT(`data_hora`, '%Y%m%d%H')
        Intervalos por dia = ... GROUP BY DATE_FORMAT(`data_hora`, '%Y%m%d')

It is efficient but I would like more options of intervals, such as 30 minutes, 7, 15 or 20 days.

I need this because I have a large number of records in a bank and it ends up getting too heavy to move all this to a graph.

  • You want to group by periods, right? What is your version of Mysql?

  • Have you tried? group by DAY(campo_data) ,WEEK(campo_data), MONTH(campo_data), YEAR(campo_data), and DATE_ADD(campo_data, INTERVAL 10 DAY); or DATE_ADD(campo_data, INTERVAL 15 MINUTE);

  • The mysql version is 15.1

  • I tried, but he shows me a result of each day independent of the value in (field_data, INTERVAL 15 MINUTE);

  • Try to create a precedent for this.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.