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 E7BB8D708C for ; Tue, 11 Oct 2005 09:50:11 -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 47322-03 for ; Tue, 11 Oct 2005 12:50:08 +0000 (GMT) Received: from sccrmhc14.comcast.net (sccrmhc14.comcast.net [63.240.76.49]) by svr1.postgresql.org (Postfix) with ESMTP id 3056BD706B for ; Tue, 11 Oct 2005 09:50:09 -0300 (ADT) Received: from dell8200 (pcp09613107pcs.rte20201.de.comcast.net[68.82.197.68]) by comcast.net (sccrmhc14) with SMTP id <2005101112501201400oolq6e>; Tue, 11 Oct 2005 12:50:13 +0000 From: "Rick Schumeyer" To: Subject: question re. count, group by, and having Date: Tue, 11 Oct 2005 08:50:10 -0400 Message-ID: <000b01c5ce62$514db090$0300a8c0@dell8200> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_000C_01C5CE40.CA3C1090" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.6626 Importance: Normal 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.001 required=5 tests=[HTML_MESSAGE=0.001] X-Spam-Level: X-Archive-Number: 200510/84 X-Sequence-Number: 22937 This is a multi-part message in MIME format. ------=_NextPart_000_000C_01C5CE40.CA3C1090 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit 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). ------=_NextPart_000_000C_01C5CE40.CA3C1090 Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable

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).

 

 

 

------=_NextPart_000_000C_01C5CE40.CA3C1090--