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

  • SEARCH
  • Home
  • 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 8650873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:55:34+00:00 2026-06-12T13:55:34+00:00

I have a CTE based query, to which I pass about 2600 4-tuple latitude/longitude

  • 0

I have a CTE based query, to which I pass about 2600 4-tuple latitude/longitude values using joins – these latitude longitude 4-tuples have been ID tagged and held in a second table called coordinates. These top left and bottom right latitude / longitude values are passed into the CTE in order to display the amount of requests (hourly) made within those coordinates for given two timestamps).- I am able to get the total requests per day within the timestamps given, that is, the total count of user requests on every specified day. (E.g. user opts to see every Wednesday or Wednesday AND Thursday etc. – between hours 11:55 and 22:04 between dates January 1 and 31, 2012 for every latitude/longitude 4-tuples I pass.) But I cannot get the rows with zcount as 0 in the results, I only get the rows with zcount > 0. My query is as below: (a note to Erwin Brandstetter if he sees this, I checked the chat room discussion about my previous question and set the coordinates values to NOT NULL due to you saying that the coordinates can be null by design, but I still have the same issue somehow. -though I may have gotten what you meant wrong since I was down with a bad case of fever back then-

WITH v AS (
   SELECT '2012-1-1 11:55:11'::timestamp AS _from -- provide times once
         ,'2012-1-31 22:02:21'::timestamp AS _to
   )
, q AS (
   SELECT c.coordinates_id
        , date_trunc('hour', t.calltime) AS stamp
        , count(*) AS zcount
   FROM   v
   JOIN   mytable t ON  t.calltime BETWEEN v._from AND v._to
                   AND (t.calltime::time >= v._from::time AND
                        t.calltime::time <= v._to::time) AND 
(extract(DOW from t.calltime) = 3)
   JOIN   coordinates c ON (t.lat, t.lon) 
                   BETWEEN (c.bottomrightlat, c.topleftlon)
                       AND (c.topleftlat, c.bottomrightlon)
   GROUP BY c.coordinates_id, date_trunc('hour', t.calltime)
   )
, cal AS (
   SELECT generate_series('2012-1-1 11:00:00'::timestamp
                        , '2012-1-31 23:00:00'::timestamp
                        , '1 hour'::interval) AS stamp)
SELECT q.coordinates_id, cal.stamp::date, sum(q.zcount) AS zcount
FROM   v, cal
LEFT   JOIN q USING (stamp)
WHERE  extract(hour from cal.stamp) BETWEEN extract(hour from v._from)
                                        AND extract(hour from v._to)
AND    extract(DOW from cal.stamp) = 3 
AND    cal.stamp >= v._from
AND    cal.stamp <= v._to
GROUP  BY 1,2
ORDER  BY 1,2;

The output I get when I execute this query is basically like this (normally I have about 10354 rows returned excluding the rows with 0 zcount, just providing two coordinates for sake of similarity):

coordinates_id  | stamp      | zcount
1               ;"2012-01-04";      2
1               ;"2012-01-11";      3
1               ;"2012-01-18";      2
2               ;"2012-01-04";      2
2               ;"2012-01-11";      3
2               ;"2012-01-18";      2

However, it should be like this where all rows with zcount 0 should also be printed out along with rows that have nonzero zcounts -E.g. January 25 with zcount 0 for the two coordinates with ID 1 and 2 should also be printed in this small portion of example-:

coordinates_id  | stamp      | zcount
1               ;"2012-01-04";      2
1               ;"2012-01-11";      3
1               ;"2012-01-18";      2
1               ;"2012-01-25";      0
2               ;"2012-01-04";      2
2               ;"2012-01-11";      3
2               ;"2012-01-18";      2
2               ;"2012-01-25";      0

Updated version with zcount values way bigger than actual. -Also rows with 0 zcount still not showing-

WITH v AS (
   SELECT '2012-1-1 11:55:11'::timestamp AS _from -- provide times once
         ,'2012-1-31 22:02:21'::timestamp AS _to
   )
, q AS (
   SELECT c.coordinates_id
        , date_trunc('hour', t.calltime) AS stamp
        , count(*) AS zcount
   FROM   v
   JOIN   mytable t ON  t.calltime BETWEEN v._from AND v._to
                   AND (t.calltime::time >= v._from::time AND
                        t.calltime::time <= v._to::time) AND 
(extract(DOW from t.calltime) = 3)
   JOIN   coordinates c ON (t.lat, t.lon) 
                   BETWEEN (c.bottomrightlat, c.topleftlon)
                       AND (c.topleftlat, c.bottomrightlon)
   GROUP BY c.coordinates_id, date_trunc('hour', t.calltime)
   )
, cal AS (
   SELECT generate_series('2012-1-1 11:00:00'::timestamp
                        , '2012-1-31 23:00:00'::timestamp
                        , '1 hour'::interval) AS stamp)
, coordst AS ( 
   SELECT coordinates_id FROM coordinates)
SELECT q.coordinates_id, cal.stamp::date, COALESCE(sum(q.zcount),0) AS zcount
FROM   v, coordst, cal
LEFT   JOIN q USING (stamp)
WHERE  extract(hour from cal.stamp) BETWEEN extract(hour from v._from)
                                        AND extract(hour from v._to)
AND    extract(DOW from cal.stamp) = 3 
AND    cal.stamp >= v._from
AND    cal.stamp <= v._to
GROUP  BY 1,2
ORDER  BY 1,2;
  • 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-12T13:55:36+00:00Added an answer on June 12, 2026 at 1:55 pm

    You need a distinct list of coordinates_id to perform a proper CROSS JOIN.
    1. Add another entry in the WITH.
    2. Add it to your JOIN (FROM v,cal, coords).
    3. Your zcount will show NULL, so COALESCE it.

    WITH v AS (
       SELECT '2012-1-1 11:55:11'::timestamp AS _from -- provide times once
             ,'2012-1-31 22:02:21'::timestamp AS _to
       )
    , q AS (
       SELECT c.coordinates_id
            , date_trunc('hour', t.calltime) AS stamp
            , count(*) AS zcount
       FROM   v
       JOIN   mytable t ON  t.calltime BETWEEN v._from AND v._to
                       AND (t.calltime::time >= v._from::time AND
                            t.calltime::time <= v._to::time) AND 
    (extract(DOW from t.calltime) = 3)
       JOIN   coordinates c ON (t.lat, t.lon) 
                       BETWEEN (c.bottomrightlat, c.topleftlon)
                           AND (c.topleftlat, c.bottomrightlon)
       GROUP BY c.coordinates_id, date_trunc('hour', t.calltime)
       )
    , cal AS (
       SELECT generate_series('2012-1-1 11:00:00'::timestamp
                            , '2012-1-31 23:00:00'::timestamp
                            , '1 hour'::interval) AS stamp)
    , coordst AS ( 
       SELECT DISTINCT coordinates_id FROM coordinates)
    SELECT coordst.coordinates_id, cal.stamp::date, COALESCE(sum(q.zcount),0) AS zcount
    FROM   v CROSS JOIN coordst CROSS JOIN cal
    LEFT   JOIN q USING q.stamp = cal.stamp AND coordst.coordinates_id = q.coordinates_id
    WHERE  extract(hour from cal.stamp) BETWEEN extract(hour from v._from)
                                            AND extract(hour from v._to)
    AND    extract(DOW from cal.stamp) = 3 
    AND    cal.stamp >= v._from
    AND    cal.stamp <= v._to
    GROUP  BY 1,2
    ORDER  BY 1,2;
    
    • 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 into which I pass about 2600 4-tuple latitude/longitude
I have a CTE-based query in which I retrieve hourly intervals between two given
I have a CTE-based query in which I retrieve hourly intervals between two given
I'm using MS SQL 2005 and I have created a CTE query to return
I have a CTE Recursive query which returns the output as expected, however It
I have a MS SQL CTE query from which I want to create a
I have an SQL query that I have written using CTE. Now, I am
I have a query with a CTE that returns multiple rows, I want to
I have the following query: WITH cte AS ( SELECT windowId, frameIndx, elemIndx, comment,
I have a query as follows: WITH CTE AS ( SELECT MIN(att.Date) [minDate] FROM

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.