For 10 minute intervals, the easiest way to proceed is to get the string format of your date, and remove anything at the right of the first digit of the 2-digit minute part. So 20-03-17 00.18.36
would be stripped to 20-03-17 00.1
. That would work well as the value to group by. For displaying purposes you could then add 0.00
to it for denoting the lower bound of the bucket, and 9.59
for the higher bound.
Now the function to convert a timestamp to a string is different in different database engines.
For Oracle it would look like this:
select substr(to_char(dat, 'YY-MM-DD HH24.MI'), 1, 13) || '0.00' bucket_start,
substr(to_char(dat, 'YY-MM-DD HH24.MI'), 1, 13) || '9.59' bucket_end,
count(*)
from mytable
group by substr(to_char(dat, 'YY-MM-DD HH24.MI'), 1, 13)
order by 1
Output:
BUCKET_START | BUCKET_END | COUNT(*)
------------------+-------------------+---------
17-03-20 00.00.00 | 17-03-20 00.09.59 | 4
17-03-20 00.10.00 | 17-03-20 00.19.59 | 2