From Mark.Fenbers@noaa.gov Mon May 16 19:45:57 2005 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 30FB65282D for ; Mon, 16 May 2005 16:45:57 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 26979-08 for ; Mon, 16 May 2005 19:45:47 +0000 (GMT) Received: from werhmsg.werh.noaa.gov (werhmsg.werh.noaa.gov [198.206.32.236]) by svr1.postgresql.org (Postfix) with ESMTP id 6E5C1528E6 for ; Mon, 16 May 2005 16:45:46 -0300 (ADT) Received: from [198.206.42.50] ([198.206.42.50]) by werhmsg.werh.noaa.gov (Netscape Messaging Server 4.15) with ESMTP id IGLLKA00.T8N for ; Mon, 16 May 2005 15:45:46 -0400 Message-ID: <4288F867.8090600@noaa.gov> Date: Mon, 16 May 2005 15:45:43 -0400 From: "Mark Fenbers" Organization: DoC/NOAA/NWS/OHRFC User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: sub-selects Content-Type: multipart/mixed; boundary="------------090105060904060804060108" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.127 tagged_above=0 required=5 tests=AWL, HTML_40_50, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200505/116 X-Sequence-Number: 21429 This is a multi-part message in MIME format. --------------090105060904060804060108 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Is there a way to make a query more efficient by executing a sub-select only once?

In a query such as:

SELECT a, (select b from c where d = e limit 1), npoints( (select b from c where d = e limit 1) )
    FROM f
    WHERE isValid( (select b from c where d = e limit 1) );

I do the same sub-select 3 times in the query.  I tried the following:

SELECT a, (select b from c where d = e limit 1) AS g, npoints( g )
    FROM f
    WHERE isValid( g );

But this gave an error regarding "column 'g' does not exist".  How can I avoid making the same sub-select 3 times?

Mark
--------------090105060904060804060108 Content-Type: text/x-vcard; charset=utf-8; name="Mark.Fenbers.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Mark.Fenbers.vcf" begin:vcard fn:Mark Fenbers n:Fenbers;Mark org:DoC/NOAA/NWS/OHRFC adr:;;1901 South SR 134;Wilmington;OH;45177-9708;USA email;internet:Mark.Fenbers@noaa.gov title:Sr. HAS Meteorologist tel;work:937-383-0430 x246 x-mozilla-html:TRUE url:http://weather.gov/ohrfc version:2.1 end:vcard --------------090105060904060804060108-- From lists@boutiquenumerique.com Mon May 16 20:36:04 2005 X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 98C49528EE for ; Mon, 16 May 2005 17:36:04 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 64714-04 for ; Mon, 16 May 2005 20:36:03 +0000 (GMT) Received: from boutiquenumerique.com (boutiquenumerique.com [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id CBE83528E7 for ; Mon, 16 May 2005 17:35:59 -0300 (ADT) Received: (qmail 18786 invoked from network); 16 May 2005 22:36:27 +0200 Received: from unknown (HELO localhost) (boutiquenumerique-lists@192.168.0.4) by boutiquenumerique.com with SMTP; 16 May 2005 22:36:27 +0200 To: "Mark Fenbers" , pgsql-sql@postgresql.org Subject: Re: sub-selects References: <4288F867.8090600@noaa.gov> Message-ID: Date: Mon, 16 May 2005 22:36:01 +0200 From: PFC Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In-Reply-To: <4288F867.8090600@noaa.gov> User-Agent: Opera M2(BETA2)/8.0 (Linux, build 987) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/117 X-Sequence-Number: 21430 SELECT foo.*, npoints( foo.g ) FROM (SELECT a, (select b from c where d = e limit 1) AS g FROM f WHERE isValid( g )) AS foo ?