Retrieving queries when the hour of starting timestamp is less than the hour of ending timestamp I have a CTE-based query in which I retrieve hourly intervals between two given timestamps. My query works as following:


Getting start and end datetimes (let's say 07-13-2011 10:21:09 and 07-31-2011 15:11:21)
Retrieving the total amount of queries within each hour interval for given timestamps. (For example in here, every day from July 13 to 31, total amount queries between hours 10 and 15 are retrieved in hourly intervals (10:21:09 to 11:00, 11:00 to 11:59, 12:00 to 12:59...,15:00 to 15:11:21) 


The query is this:

;WITH cal AS 
    (
        SELECT generate_series('2-2-2011 00:00:00'::timestamp, 
            '1-4-2012 05:00:00'::timestamp, 
            '1 hour'::interval) AS stamp
    )
, qqq AS (
        SELECT date_trunc('hour', calltime) AS stamp
            , count(*) AS zcount
        FROM mytable
    WHERE calltime &gt;= '13-7-2011 10:21:09' 
        AND calltime &lt;= '31-7-2011 15:11:21' 
        AND calltime::time &gt;= '10:00:00' 
        AND calltime::time &lt;= '15:59:59' 
        AND date_part('hour', calltime) &gt;= 8 
        AND date_part('hour', calltime) &lt;= 15
    GROUP BY date_trunc('hour', calltime)
)
SELECT cal.stamp
    , COALESCE (qqq.zcount, 0) AS zcount
FROM cal
    LEFT JOIN qqq ON cal.stamp = qqq.stamp
WHERE cal.stamp &gt;= '13-7-2011 10:00:00' 
    AND cal.stamp &lt;= '31-7-2011 15:11:21' 
    AND date_part('hour', cal.stamp) &gt;= 10 
    AND date_part('hour', cal.stamp) &lt;= 15
ORDER BY stamp ASC;


And a snippet of the result is like this: (zcount values vary, this is just a by-product of copy-and-paste during editing the question)

stamp                 zcount
"2011-07-13 10:00:00"  123
"2011-07-13 11:00:00"  338
"2011-07-13 12:00:00"  184
"2011-07-13 13:00:00"  298
"2011-07-13 14:00:00"  162
"2011-07-13 15:00:00"  293
"2011-07-14 10:00:00"  216
"2011-07-14 11:00:00"  392
"2011-07-14 12:00:00"  268
"2011-07-14 13:00:00"  483
"2011-07-14 14:00:00"  327
"2011-07-14 15:00:00"  419
.
.
.
"2011-07-31 10:00:00"  216
"2011-07-31 11:00:00"  392
"2011-07-31 12:00:00"  268
"2011-07-31 13:00:00"  483
"2011-07-31 14:00:00"  327
"2011-07-31 15:00:00"  419


My query works well for inputs in which the hour of the first timestamp is smaller than second one -e.g. in here, 10 AM for first timestamp's hour and 3 PM for second timestamp's hour.

However, when I want to retrieve total counts of query per every hour interval for inputs such as 07-13-2011 22:11:43 and 07-31-2011 04:06:04, - so that I get query counts between 10:11:43 pm to 10:59pm, 11pm to 11:59pm,...,3:00am to 3:59, 4:00am to 4:06:04am for every single day,  I'm having problems. 

I need to retrieve the total counts of queries like the following for each given day in input:

    stamp                 zcount
    "2011-07-13 22:00:00"  123
    "2011-07-13 23:00:00"  338
    "2011-07-14 00:00:00"  184
    "2011-07-13 01:00:00"  298
    "2011-07-13 02:00:00"  162
    "2011-07-13 03:00:00"  293
    "2011-07-14 04:00:00"  216
    "2011-07-14 22:00:00"  392
    "2011-07-14 23:00:00"  268
    "2011-07-15 00:00:00"  483
    "2011-07-15 01:00:00"  327
    "2011-07-15 02:00:00"  419
    "2011-07-15 03:00:00"  121
    "2011-07-15 04:00:00"  231
     .
     . 
    "2011-07-30 22:00:00"  392
    "2011-07-30 23:00:00"  268
    "2011-07-31 00:00:00"  483
    "2011-07-31 01:00:00"  327
    "2011-07-31 02:00:00"  419
    "2011-07-31 03:00:00"  544
    "2011-07-31 04:00:00"  231


What do I need to change in my query in order to make sure it works for both timestamp cases as I presented above? 

I currently use a workaround for the second case but I would like to handle all of it in a single query.
As Erwin Brandstetter pointed out, cross post from stackoverflow.com/q/12379598/939860. Solved.