X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 9240CD1B4F9 for ; Sun, 14 Sep 2003 04:38:55 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 32931-10 for ; Sun, 14 Sep 2003 01:38:45 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 04082D1B4F5 for ; Sun, 14 Sep 2003 01:38:44 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h8E4chUe007111; Sun, 14 Sep 2003 00:38:43 -0400 (EDT) To: "Richard Sydney-Smith" Cc: pgsql-sql@postgresql.org Subject: Re: Use derived expression in select statement In-reply-to: <000f01c37a78$4ea53560$076c32d2@athlon2000> References: <000f01c37a78$4ea53560$076c32d2@athlon2000> Comments: In-reply-to "Richard Sydney-Smith" message dated "Sun, 14 Sep 2003 12:25:58 +0800" Date: Sun, 14 Sep 2003 00:38:42 -0400 Message-ID: <7110.1063514322@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/115 X-Sequence-Number: 15093 "Richard Sydney-Smith" writes: > select dy_c , dy_sect as tsect, (cy_bfwd + dy_p1T4) as curr_bal from fclitot > where dy_yr = 0 and (curr_bal) <-0.005 > In postgres it tells me curr_bal is not found. As it should --- this is completely illegal according to the SQL standard. It's not even well-defined. The SQL evaluation model is that WHERE clause processing is done *before* evaluation of the select list. The usual way to avoid writing common subexpressions is to use a sub-select, for example select dy_c , dy_sect as tsect, curr_bal from (select *, (cy_bfwd + dy_p1T4) as curr_bal from fclitot) as ss where dy_yr = 0 and (curr_bal) <-0.005 This doesn't necessarily save you from evaluating the curr_bal expression twice, mind you. It just saves you from writing it out twice. regards, tom lane