X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AD6B0D7725 for ; Tue, 11 Oct 2005 18:27:52 -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 67938-10 for ; Tue, 11 Oct 2005 21:27:48 +0000 (GMT) Received: from web8606.mail.in.yahoo.com (web8606.mail.in.yahoo.com [202.43.219.81]) by svr1.postgresql.org (Postfix) with SMTP id 297D5D771F for ; Tue, 11 Oct 2005 18:27:48 -0300 (ADT) Received: (qmail 43242 invoked by uid 60001); 11 Oct 2005 21:27:48 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.co.in; h=Message-ID:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=UL+cCIaOi+WvJkjHXAl3RBIPcW0COSCMZi5WdzJDz1kpMX5B2g1sxxysFuNKD6A09wLTgEr+MmPY8eY/iKEeZYRg66n0xHXiNVBeLBaIjt4cPfEWMp0ANuvQvD1ybuQHBKODZq39+xspFHu3448zXENyVI/FDlLL8rHVaDL9aCY= ; Message-ID: <20051011212748.43240.qmail@web8606.mail.in.yahoo.com> Received: from [12.109.150.2] by web8606.mail.in.yahoo.com via HTTP; Tue, 11 Oct 2005 22:27:48 BST Date: Tue, 11 Oct 2005 22:27:48 +0100 (BST) From: Muralidharan Ramakrishnan Subject: Re: question re. count, group by, and having To: Rick Schumeyer , pgsql-sql@postgresql.org In-Reply-To: <000b01c5ce62$514db090$0300a8c0@dell8200> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-555375571-1129066068=:41045" Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.751 required=5 tests=[DNS_FROM_RFC_ABUSE=0.374, DNS_FROM_RFC_POST=1.376, HTML_MESSAGE=0.001] X-Spam-Level: * X-Archive-Number: 200510/98 X-Sequence-Number: 22951 --0-555375571-1129066068=:41045 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Hi Logically HAVING is executed after the GROUP BY and it must contain only the columns in the GROUP BY or aggregated function. select state, count(state) as c from t group by state having c > 5 The above query grouped only on state and HAVING can be used only with the column state or the aggregated function count(state). Regards, R.Muralidharan Rick Schumeyer wrote: The following query returns an error (“column c does not exist”) in pg 8.0.3: (The column ‘state’ is the two letter abbreviation for a US state) -- get the number of rows for each state; list in descending order; include only states with at least 6 rows select state, count(state) as c from t group by state having c > 5 order by c desc; -- gives error If I leave the having clause out, I get the expected results: select state, count(state) as c from t group by state order by c desc; -- this works Is this a bug or a feature? I’m not sure why I can use ‘c’ in the order by clause but not the having clause. pg is much happier with the full “having count(state) > 5”. Will this cause count to be evaluated twice? If it matters, state is varchar(2). --------------------------------- Yahoo! India Matrimony: Find your partner online. --0-555375571-1129066068=:41045 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: 8bit
Hi
 
  Logically HAVING is executed after the GROUP BY and it must contain only the columns in the GROUP BY or aggregated function.
 
select state, count(state) as c from t group by state having c > 5
 
The above query grouped only on state and HAVING can be used only with the column state or the aggregated function count(state).
 
Regards,
R.Muralidharan
 

Rick Schumeyer <rschumeyer@ieee.org> wrote:

The following query returns an error (“column c does not exist”) in pg 8.0.3:

 

(The column ‘state’ is the two letter abbreviation for a US state)

 

-- get the number of rows for each state; list in descending order; include only states with at least 6 rows

select state, count(state) as c from t group by state having c > 5 order by c desc; -- gives error

 

If I leave the having clause out, I get the expected results:

 

select state, count(state) as c from t group by state order by c desc; -- this works

 

Is this a bug or a feature?  I’m not sure why I can use ‘c’ in the order by clause but not the having clause.  pg is much happier with the full “having count(state) > 5”.  Will this cause count to be evaluated twice?

 

If it matters, state is varchar(2).

 

 

 


Yahoo! India Matrimony: Find your partner online. --0-555375571-1129066068=:41045--