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 1CF9532A549 for ; Thu, 21 Oct 2004 02:31:50 +0100 (BST) 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 87012-06 for ; Thu, 21 Oct 2004 01:31:37 +0000 (GMT) Received: from millenium.mst.co.jp (unknown [210.230.185.241]) by svr1.postgresql.org (Postfix) with ESMTP id 6107232A526 for ; Thu, 21 Oct 2004 02:31:39 +0100 (BST) Received: from mst1x5r347kymb (lc12114 [192.168.1.114]) by millenium.mst.co.jp (8.11.6p2/3.7W) with SMTP id i9L1V5q02088; Thu, 21 Oct 2004 10:31:05 +0900 Message-ID: <008f01c4b70d$a22184a0$7201a8c0@mst1x5r347kymb> From: "Iain" To: "Rod Taylor" , "Philippe Lang" Cc: References: <6C0CF58A187DA5479245E0830AF84F42080442@poweredge.attiksystem.ch> <1098277537.747.91.camel@home> Subject: Re: Reuse previously calculated column in sql query? Date: Thu, 21 Oct 2004 10:31:00 +0900 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-2022-jp"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/149 X-Sequence-Number: 19252 Using the the sub-select is one way, but there is another way. If the function can be declared as strict or immutable the you can call it as many times as you like in a single transaction and it will only be evaluated once. As far as I know this does work as advertised. Check the SQL commands reference section of the manual for CREATE FUNCTION Regards Iain ----- Original Message ----- From: "Rod Taylor" To: "Philippe Lang" Cc: Sent: Wednesday, October 20, 2004 10:05 PM Subject: Re: [SQL] Reuse previously calculated column in sql query? > On Wed, 2004-10-20 at 08:50, Philippe Lang wrote: >> Hello, >> >> Is it possible to reuse a previously calculated column in a following >> column, like: > > SELECT col1 > , col2 > , col2 * 0.75 AS col3 > FROM (SELECT foo.val1 AS col1 > , long_calculation(foo.val1) AS col2 > FROM foo) AS tab; > > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html