Grouping by timestamp, how to return 0 when there's no record

Поиск
Список
Период
Сортировка
От Koen Van Impe
Тема Grouping by timestamp, how to return 0 when there's no record
Дата
Msg-id 4E771315.3050409@belnet.be
обсуждение исходный текст
Ответы Re: Grouping by timestamp, how to return 0 when there's no record
Список pgsql-general
Hello,

I'm trying to write a query that groups records by hour.
This works fine but when there are no records for a specific hour the
query does not return a result (this seems 'logic') and I'd like it to
return '0'. I suspect I should play around with 'interval' or something
but I can't get it to work.

I'd like to see a resultset similar to this
 20  |  2011-09-12 12:00:00
  7  |  2011-09-12 13:00:00
  0  |  2011-09-12 14:00:00
  14 |  2011-09-12 15:00:00

The current query is
SELECT COUNT(*) AS qt, DATE_TRUNC('hour',timestamp) as dfilter FROM
record WHERE record.timestamp BETWEEN (CURRENT_TIMESTAMP + INTERVAL '-7
day') and (CURRENT_TIMESTAMP) GROUP BY dfilter ORDER BY dfilter

Any help is highly appreciated.

kr,

koen

В списке pgsql-general по дате отправления:

Предыдущее
От: Rueegg Alexander
Дата:
Сообщение: Re: Dblink upgrade from 8.4 to 9.1
Следующее
От: Vincent de Phily
Дата:
Сообщение: Re: duplicate sequence, it is possible?