Search Postgresql Archives

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux