PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2012, 02:24 PM
dud dud is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 6 dud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 6 m 21 sec
Reputation Power: 0
How to call this CTE for multiple data?

I have a CTE that takes top left and bottom right latitude/longitude values along with a start and end date and it then calculates the amount of user requests that came from those coordinates per hourly intervals between the given start and end date. However, I want to execute this query for about 2600 seperate 4-tuples of lat/lon corner values instead of typing them in one-by-one. How would I do that? Thanks.


Code:
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 >= '2011-02-13 11:59:11' 
    AND calltime <= '2012-02-13 22:02:21'
    AND (calltime::time >= '11:59:11' 
    AND calltime::time <= '22:02:21')
    AND ((extract(DOW from calltime) = 3) /*OR (extract(DOW from calltime) = 5)*/)
    AND lat BETWEEN '40' AND '42' 
    AND lon BETWEEN '28' AND '30'
 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 >= '2011-02-13 11:00:00' 
  AND cal.stamp <= '2012-02-13 22:02:21' 
  AND ((extract(DOW from cal.stamp) = 3) /*OR (extract(DOW from cal.stamp) = 5)*/)
  AND (
    extract ('hour' from cal.stamp) >= extract ('hour' from '2011-02-13 11:00:00'::timestamp) AND
    extract ('hour' from cal.stamp) <= extract ('hour' from '2012-02-13 22:02:21'::timestamp)
  )
ORDER BY stamp ASC;



And sample output is like this (the hours represent an hourly interval 22:00 means time between 22:00:00 and 22:59:59 etc.)

Code:
calltime                      zcount
"2011-02-16 11:00:00"        0
"2011-02-16 12:00:00"       70
"2011-02-16 13:00:00"     175
"2011-02-16 14:00:00"       97
"2011-02-16 15:00:00"      167
.
.
.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > How to call this CTE for multiple data?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap