I have a table with many rows so when I make this query:
SELECT
date_trunc('hour', fecha) AS momento
,(extract(minute FROM fecha)::int / 5) AS min
,count(fecha) AS cuenta
FROM table1
WHERE fk_id_busqueda=$id_busqueda
GROUP BY 1, 2
ORDER BY 1, 2;
It takes a lot of time. One solution that I am thinking is make a postgres function where I can order the data by date and then sum one everytime momento of the previous row is the same than the actual one with a loop. And when is different then reset the value and pass to the Next row. The idea is that the function recives id_busqueda (int).
The output should be like this:
2013-01-20 00:00:00 | 0 | 16
2013-01-20 00:00:00 | 1 | 30
2013-01-20 00:00:00 | 2 | 27
2013-01-20 00:00:00 | 3 | 30
2013-01-20 00:00:00 | 4 | 33
2013-01-20 00:00:00 | 5 | 21
2013-01-20 00:00:00 | 6 | 27
2013-01-20 00:00:00 | 7 | 27
2013-01-20 00:00:00 | 8 | 19
2013-01-20 00:00:00 | 9 | 13
2013-01-20 00:00:00 | 10 | 17
2013-01-20 00:00:00 | 11 | 24
2013-01-20 01:00:00 | 0 | 12
2013-01-20 01:00:00 | 1 | 15
2013-01-20 01:00:00 | 2 | 25
2013-01-20 01:00:00 | 3 | 19
2013-01-20 01:00:00 | 4 | 10
2013-01-20 01:00:00 | 5 | 12
2013-01-20 01:00:00 | 6 | 13
fk_id_busquedacolumn?