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

From: Diego Augusto Molina <diegoaugustomolina(at)gmail(dot)com>
To: Alban Hertroys <haramrae(at)gmail(dot)com>
Cc: koen(dot)vanimpe(at)belnet(dot)be, pgsql-general(at)postgresql(dot)org
Subject: Re: Grouping by timestamp, how to return 0 when there's no record
Date: 2011-09-19 12:08:27
Message-ID: CAGOxLdFcpcy4XTezuGXw7gnPf95UY4pqJhVC5BdD11E6aifrfQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi, this may be a start:

-- This will make our day better :)
with base_query (tstmp) as (
select DATE_TRUNC('hour',timestamp) as tstmp
FROM record
WHERE
record.timestamp BETWEEN ( CURRENT_TIMESTAMP + INTERVAL '-7 day') and
(CURRENT_TIMESTAMP) -- this I don't understand
)
-- The following will make the same as your query
select count(*) as qt, tstmp as dfilter from base_query
-- And this adds the zero's
union
select 0 as qt, min(tstmp) + interval generate_series( 0, max(tstmp) -
min(tstmp) ) || ' hours' as dfilter
from base_query
where dfilter not in tstmp; -- Only for those who don't!

Don't forget to tell us how you ended up!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Henry Drexler 2011-09-19 13:10:20 General guidance if there is an in dadabase solution or should stay as excel vba solution.
Previous Message Alban Hertroys 2011-09-19 11:55:42 Re: Grouping by timestamp, how to return 0 when there's no record