To group by an interval, truncate the date in the unit of that interval (minute, in your case), divide by the total of a day and disappear with the date and time:
(trunc(current_date, 'hh')+trunc(to_char(current_date,'mi')/5)*5/1440)
In this SQL Fiddle I created a table with some timestamps for testing, in the name column d
:
> select * from datas;
| D |
|-----------------------|
| 2017-10-01 08:00:00.0 |
| 2017-10-01 08:01:00.0 |
| 2017-10-01 08:03:00.0 |
| 2017-10-01 08:07:00.0 |
| 2017-10-01 08:08:00.0 |
| 2017-10-01 08:09:59.0 |
| 2017-10-01 08:11:00.0 |
| 2017-10-01 08:11:15.0 |
| 2017-10-01 08:13:00.0 |
| 2017-10-01 08:17:00.0 |
For this table I apply the above method, both for the five-minute period and for the clause GROUP BY
and to a window in order to obtain the numbering records that appears in your question:
select
row_number() over (order by (trunc(d, 'hh')+trunc(to_char(d,'mi')/5)*5/1440)) as " ",
to_char((trunc(d, 'hh')+trunc(to_char(d,'mi')/5)*5/1440), 'HH24:MI') as "Hora",
count(*) as "Quantidade"
from datas
group by (trunc(d, 'hh')+trunc(to_char(d,'mi')/5)*5/1440);
| | Hora | Quantidade |
|---|-------|------------|
| 1 | 08:00 | 3 |
| 2 | 08:05 | 3 |
| 3 | 08:10 | 3 |
| 4 | 08:15 | 1 |
But how do I group the date field every 5 minutes for example? , this select will only group dates exactly equal
– Pilati
group by will group yes exact dates equal
– GabrielLocalhost
Yes, but I want to group dates by period, and not exactly equal. that way will not return what I need
– Pilati
http://stackoverflow.com/questions/19309959/oracle-plsql-truncate-datetimes-to-15-min-blocks a combination of trunc and other functions.
– Motta