Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f7OJwnf88751 for ; Fri, 24 Aug 2001 15:58:51 -0400 (EDT) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.1) with HTTP id 107962; Fri, 24 Aug 2001 12:58:40 -0700 From: "Josh Berkus" Subject: Re: WHERE on an alias To: Joseph Shraibman Cc: pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.4.1 Date: Fri, 24 Aug 2001 12:58:40 -0700 Message-ID: In-Reply-To: <3B8698E7.4080106@selectacast.net> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200108/307 X-Sequence-Number: 4554 Joseph, > playpen=# select a, sum(b) as dsum from taba where dsum > 5 group > by(a); > ERROR: Attribute 'dsum' not found > > Why can we GROUP BY on an alias but not do a WHERE on an alias? I > have a subselect that > explain shows is being run twice if I have to put it in the WHERE > clause. Ah, but you are not trying to do a WHERE on an alias ... you are trying to do a WHERE on an *aggregate*, which is a different thing. Consult your favorite SQL manual; the construction you want is: SELECT a, sum(b) as dsum FROM taba GROUP BY a HAVING dsum > 5; -Josh Berkus ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco