Is it possible to group a range of values?

Asked

Viewed 270 times

1

I made the following consultation:

`select t.net, t.hora, t.`data`, t.`local` from domingos t
join(
select net, updated, count(*) from domingos
where `data` = '07/05/2017'
group by updated, net
having count(*) > 4
) as u
on t.updated = u.updated
and t.net = u.net
group by t.net, t.hora, t.`data`, t.`local`
order by hora;`

This query returns the values in the figure below

inserir a descrição da imagem aqui

Is there any parameter for group by that makes it possible to group the values of the column hora whereas, for example, the values 00:34:00 to 00:36:00 are represented by only one value?

  • then you would have to truncate the time, into whatever shape you want, or create a case for your condition.

  • No, because different times refer to different devices, but different devices can have the same 'net'. Grouping by net would make me lose information about the number of devices. You see?

  • Okay... I’ll post

1 answer

2


Try this:

SELECT t.net, u.hh, t.`data`, t.`local`
FROM domingos t
JOIN (
    SELECT net, updated, HOUR(hora) AS hh, COUNT(*)
    FROM domingos
    WHERE `data` = '07/05/2017'
    GROUP BY updated, net
    HAVING COUNT(*) > 4
) AS u
ON t.updated = u.updated
AND t.net = u.net
GROUP BY t.net, u.hh, t.`data`, t.`local`
ORDER BY hora;

I mean, instead of using t.hora in the GROUP BY and in the SELECT, use the u.hh which is the HOUR(hora).

  • If I’m not mistaken t.hora can’t stay in the select if you won’t list them all.:)

  • @Marconi In the database there are several repeated values for hora, I used group by to know only one of them, as well as the other columns.

  • @Marconi is right. I edited the answer.

  • 1

    @Victorstafusa I believe this is the answer.

  • @Victorstafusa Is giving error when using HOUR(t.hour) on group by. When I use only on select results. However, the result is only the hour, IE, disregard the minutes and seconds, the grouping per hour would make me lose information. There are how to group for minutes?

  • @Frankqueiroz Answer edited. Tell me if it works like this. As for the minutes, you can also put a field for the MINUTE(hora).

  • Perfect! In the code above just put hour(hora) in the group by and hh in the order by that works perfectly. With the tip of minute(hora) my problem will be solved.

Show 2 more comments

Browser other questions tagged

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