Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TJrSV-0005Nn-Hl for pgsql-sql@postgresql.org; Thu, 04 Oct 2012 19:51:47 +0000 Received: from smtp107.prem.mail.ac4.yahoo.com ([76.13.13.46]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TJrSS-0007rJ-8Z for pgsql-sql@postgresql.org; Thu, 04 Oct 2012 19:51:46 +0000 Received: (qmail 27787 invoked from network); 4 Oct 2012 19:51:42 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=WGlZn+rnZWieeuZaIdeHo0nfLPdCp9iBu3yXR+L1o7oF/lltxsEwBARIG9bkOVqhRuP3ZebMKUI8ZFYhT0RiGtqQkS0Kd5bPFSoytcim3NdhNALLaC0GLUUB4QBHHHnjBbW5iijJsX8Wz4aJQP4vWm3wsaMi6Quk5njtQB/ULPs= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1349380302; bh=WIOefV7wfHTUhpPf3laYu/53OJ0TSdItCWrx94EV7Nk=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=mnj576FGVy4qbb/pef25HiMFVIJlRhaa67lL0uU3TzwyilDbghKg7ZgwBbDwxAEOcPsZqlo4vt7sdIf8KoV+P5RLucd5XQNqhGoR8EGlHh6g0o4owmmNwsmVy6HjHxqeSVlWZvKhnautREYTctyW2udjiO9KGE0kHrTgjjrucuU= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: LPxm1TsVM1l28JK7yHElGsMU8b9cO4jzxkZ9Jd1z1OipvJj py2aWtnQ_je8P17XpynPH2m3lq1nxQ4dp2.O00bvlHeRhqzxokc_.E34fpLh 7UtlkhFF_aXgqR2ud3Tw1WkHwaFtInXcJoD.R8O4.xVQetpQ_RztnBrWeFqy YJd8pHC_pPcyGRkar7uPu.7nK_xSKn6CrVaR.n6s5TD.KHLbEe2Y5SrbqAnF UX2OERboRuqV6BlNGCeNJg50mmej.rTezFHNuB.7MitJSqzxlzR.MvyYqQje dfGLnDdDRy0qnSNfzWsxo6sdIgCYbVXrRV0zwwFlSm9lMZL_ErNBqO5.9xDB md3ywlCiEM0bjYw1bJrsnrk8lKRVLJl_XcAW4Oap26LmgbpV1XePo5CwUPSC wnYjof3.Yki594yFUugoPF.B1QCMk77e3kUbAwZ0eFCneeLm6P8RlHH.7gUC 4qjXg X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp107.prem.mail.ac4.yahoo.com with SMTP; 04 Oct 2012 12:51:42 -0700 PDT From: "David Johnston" To: "'air'" , References: <1349379109760-5726661.post@n5.nabble.com> In-Reply-To: <1349379109760-5726661.post@n5.nabble.com> Subject: Re: Calling the CTE for multiple inputs Date: Thu, 4 Oct 2012 15:51:24 -0400 Message-ID: <021401cda269$a38a5470$ea9efd50$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQIQ6GT3dnJg7Cu+a14FqHWFSDCiqZcjALmA Content-Language: en-us X-Pg-Spam-Score: -4.1 (----) X-Archive-Number: 201210/20 X-Sequence-Number: 36891 > -----Original Message----- > From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql- > owner@postgresql.org] On Behalf Of air > Sent: Thursday, October 04, 2012 3:32 PM > To: pgsql-sql@postgresql.org > Subject: [SQL] Calling the CTE for multiple inputs > > 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? The code is as below: > > AND lat BETWEEN '40' AND '42' > AND lon BETWEEN '28' AND '30' I don't really follow but if I understand correctly you want to generate 2600 distinct rows containing values like (40, 42, 28, 30)? You could use "generate_series()" to generate each individual number along with a row_number and then join them all together: SELECT lat_low, lat_high, long_low, long_high FROM (SELECT ROW_NUMBER() OVER () AS index, generate_series(...) AS lat_low) lat_low_rel NATURAL JOIN (SELECT ROW_NUMBER() OVER () AS index, generate_series(...) AS lat_high) lat_high_rel NATURAL JOIN (SELECT ROW_NUMBER() OVER () AS index, generate_series(...) AS long_low) long_low_rel NATURAL JOIN (SELECT ROW_NUMBER() OVER () AS index, generate_series(...) AS long_high) long_high_rel You may (probably will) need to move the generate_series into a FROM clause in the sub-query but the concept holds. Then in the main query you'd simply... AND lat BETWEEN lat_low AND lat_high AND lon BETWEEN long_low AND long_high HTH David J.