>I'm trying to make a query that, given N and a date, gives me the interval of N hours with the max(sum(...)).
select sum(i) as s, timestamp '2010-06-16 00:00:00' + extract(hour from d)::integer/3*3 * interval '1 hour' as sumd from p group by extract(hour from d)::integer/3 where d = '2010-06-16 00:00:00' order by s desc limit 1;
is this what you're looking for?