Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:02:33+00:00 2026-06-10T03:02:33+00:00

I have a CTE-based query in which I retrieve hourly intervals between two given

  • 0

I have a CTE-based query in which I retrieve hourly intervals between two given timespans. My query works as following:

Getting start and end datetimes (let’s say 07-13-2011 00:21:09 and 07-31-2011 21:11:21) get the hourly total query values between the hourly intervals (in here it’s from 00 to 21, a total of 21 hours but this is parametric and depends on the hours I give for the inputs) for each day.

This query works well for inputs in which the hour of the first timestamp is smaller than second one -e.g. 03 AM for first timestamp’s hour and 07 AM for second timestamp’s hour but there is a problem. When I want to retrieve total counts of query for inputs such as 07-13-2011 22:11:43 and 07-25-2011 04:06:04, I’m having problems. I need to retrieve the total counts of queries like the following:

07-13-2011 22:00:00    143 //representing the total amounts of queries 22:11:43 - 22:59:59 interval-
07-13-2011 23:00:00    121 //representing the total amounts of queries in 23:00:00  -23:59:59 interval-
07-14-2011 00:00:00     65 //00:00:00  - 00:59:59 interval
07-14-2011 01:00:00     51 //01:00:00  - 01:59:59 interval...
.
.
.
07-14-2011 04:00:00  22  //query amount for 04:00:00 - 04:06:04 interval

and so on. What do I need to do in addition to the CTE query I have written below?

WITH cal AS (
    SELECT generate_series('2011-02-02 00:00:00'::timestamp
                         , '2012-04-01 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 >= '07-13-2011 22:00:00'
    AND calltime <='07-31-2011 04:33:21' 
    AND calltime::time >= '22:00:00' 
    AND calltime::time <= '04:33:21'
    -- this calltime::time part obviously doesn't work due to common sense and logic
    -- edited it to show what I try to mean 
    AND date_part('hour', calltime) >= 0
    AND date_part('hour', calltime) <= 21
    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 >= '07-13-2011 22:00:00'
AND cal.stamp<='07-31-2011 04:33:21'
AND date_part('hour', cal.stamp) >= 0
AND date_part('hour', cal.stamp) <= 21
ORDER BY stamp ASC;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:02:35+00:00Added an answer on June 10, 2026 at 3:02 am

    Consider this amended version:

    WITH param AS (
       SELECT '2011-07-13 22:11:43'::timestamp AS start -- supply start / stop once
             ,'2011-07-25 04:06:04'::timestamp AS stop
       )
       , cal   AS (
       SELECT generate_series(date_trunc('hour', p.start)
                             ,date_trunc('hour', p.stop + interval '1h')
                             ,interval '1h') AS h
       FROM param p
       )
       , q    AS (
       SELECT date_trunc('hour', calltime) AS h
             ,count(*) AS ct
       FROM   mytable
             ,param p
       WHERE  calltime >= p.start
       AND    calltime <= p.stop
       -- uncomment if you actually want to exclude hours 22 & 23 (?)
       -- AND    extract('hour' FROM calltime) BETWEEN 0 AND 21
       GROUP  BY 1
       )
    SELECT cal.h, COALESCE(q.ct, 0) AS ct
    FROM   cal
    LEFT   JOIN q USING (h)
    -- uncomment if you actually want to exclude hours 22 & 23 (?)
    -- WHERE  extract('hour' FROM cal.h) BETWEEN 0 AND 21
    ORDER  BY 1;
    

    The major change is to generate hours from the actual time-span right away.
    Removed a couple of unneeded conditions.
    Using ISO 8601 format for timestamps (That works with every locale).

    Find more context and links at this related answer. The only difference: over there it’s about a running count.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CTE-based query in which I retrieve hourly intervals between two given
I have the following query: WITH cte AS ( SELECT windowId, frameIndx, elemIndx, comment,
I need a difference between two values based on their row number. The CTE
I have a MS SQL CTE query from which I want to create a
I'm using MS SQL 2005 and I have created a CTE query to return
I have a query with a CTE that returns multiple rows, I want to
I have a query as follows: WITH CTE AS ( SELECT MIN(att.Date) [minDate] FROM
I have the following CTE SQL WITH Tasks AS ( SELECT TaskID, ParentTaskID, CAST(SortKey
I have the following query: ;WITH valRules AS ( SELECT vr.valRuleID, Count(*) AS totalRows,
Say that have the following CTE that returns the level of some tree data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.