Received: from p1.selectacast.net (p1.selectacast.net [64.15.230.162]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f7OKIrf94355 for ; Fri, 24 Aug 2001 16:18:53 -0400 (EDT) (envelope-from jks@selectacast.net) Received: from selectacast.net (IDENT:jks@d1 [64.7.9.58]) by p1.selectacast.net (8.9.3/8.9.3) with ESMTP id QAA10574; Fri, 24 Aug 2001 16:18:51 -0400 Message-ID: <3B86B6AA.3020309@selectacast.net> Date: Fri, 24 Aug 2001 16:18:50 -0400 From: Joseph Shraibman Organization: Xtenit, Inc. http://www.xtenit.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.3) Gecko/20010801 X-Accept-Language: en-us MIME-Version: 1.0 To: Josh Berkus CC: pgsql-sql@postgresql.org Subject: Re: WHERE on an alias References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200108/304 X-Sequence-Number: 4550 playpen=# SELECT a, sum(b) as dsum playpen-# FROM taba playpen-# GROUP BY a playpen-# HAVING dsum > 5; ERROR: Attribute 'dsum' not found HAVING does not work either. In fact this example is in the pg docs for SELECT: SELECT kind, SUM(len) AS total FROM films GROUP BY kind HAVING SUM(len) < INTERVAL '5 hour'; Josh Berkus wrote: > 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; > > -- Joseph Shraibman jks@selectacast.net Increase signal to noise ratio. http://www.targabot.com