Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TJygW-0002px-2Y for pgsql-sql@postgresql.org; Fri, 05 Oct 2012 03:34:44 +0000 Received: from mail-pa0-f46.google.com ([209.85.220.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TJygT-0005jI-9y for pgsql-sql@postgresql.org; Fri, 05 Oct 2012 03:34:43 +0000 Received: by mail-pa0-f46.google.com with SMTP id hz1so1181827pad.19 for ; Thu, 04 Oct 2012 20:34:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=bbogtNPGgznLeCG4/YJGXNh1AL9kUUXI7xfH+5S5KcQ=; b=E5PDq77tV4UmIeiIvwA2Sj/NTuMbfZbUfC80dBpBkv19Xnb8nRJMwzXv48AtAx83us wfp+w1HB+HqVLLoA62vEfSoeY8yG/PFxzFg1lUjyBIDxFAncAWlsRZjAyaZlnDChzUQz B/UT7npyohJ/TjJpHrvANwVfwn6ywerhuHjoOqoensc+qFKjHf1zfgTG1bpAgfI8mXvy UEDufU/hA07WLTNkxRjVN8MTD+caXGUDW6bmoeCUeuqpW2rKJ2Ylv4d/Qt8zVq5VMGzU sajwFimC5MM+6JtvCYIevIZzjcRxaKGnW/v749BI7yK1WMTnaVx6bTQ07Dz+HTsVl/Tc QGsQ== Received: by 10.66.79.36 with SMTP id g4mr18061142pax.67.1349408079008; Thu, 04 Oct 2012 20:34:39 -0700 (PDT) Received: from ayaki.localdomain (office1.postnewspapers.com.au. [150.101.171.128]) by mx.google.com with ESMTPS id kt2sm5330920pbc.73.2012.10.04.20.34.36 (version=SSLv3 cipher=OTHER); Thu, 04 Oct 2012 20:34:38 -0700 (PDT) Message-ID: <506E5549.2070204@ringerc.id.au> Date: Fri, 05 Oct 2012 11:34:33 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120717 Thunderbird/14.0 MIME-Version: 1.0 To: air CC: pgsql-sql@postgresql.org Subject: Re: Calling the CTE for multiple inputs References: <1349379109760-5726661.post@n5.nabble.com> In-Reply-To: <1349379109760-5726661.post@n5.nabble.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQn+lbtUW0AFgJRK7tTrykGYqGQVEk48xknXpQvGeYzyDuara3PuxQn1HSw/bel9JAVdKwhN X-Pg-Spam-Score: -0.8 (/) X-Archive-Number: 201210/22 X-Sequence-Number: 36893 On 10/05/2012 03:31 AM, air wrote: > 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: Sometimes it's easiest to just wrap it in an SQL function. CREATE OR REPLACE FUNCTION some_expr( lat_low IN integer, lat_high IN integer, lon_low IN integer, lon_high IN integer, calltime OUT timestamptz, zcount OUT integer) returns setof record as $$ -- ... query text here, using $1 through $4 to refer to parameters $$ LANGUAGE 'SQL'; ... then invoke with something like (untested, from memory): SELECT (some_expr(lat_low, lat_high, lon_log, lon_high).*) FROM table_containing_lat_lon_pairs; Alternately you may be able to rephrase the `qqq` part as a `join` on a table containing the lat/lon pairs and include those pairs in `qqq`'s output as well as the rest. You then use those in the outer query where required. Without a schema to test with and some understanding of what the query does it's hard to say exactly. Wrapping it in a function is likely to be less efficient, but probably easier. -- Craig Ringer > > > 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 the sample output for the query above: > > 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 > . > . > . > > > > > > -- > View this message in context: http://postgresql.1045698.n5.nabble.com/Calling-the-CTE-for-multiple-inputs-tp5726661.html > Sent from the PostgreSQL - sql mailing list archive at Nabble.com. > >