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 C91BAD6F01 for ; Tue, 11 Oct 2005 10:07:04 -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 99559-04 for ; Tue, 11 Oct 2005 13:07:00 +0000 (GMT) Received: from wimpy.net.nih.gov (mailfwd.nih.gov [128.231.88.106]) by svr1.postgresql.org (Postfix) with ESMTP id BCBA0D6E41 for ; Tue, 11 Oct 2005 10:07:02 -0300 (ADT) Received: from wimpy.net.nih.gov (localhost [127.0.0.1]) by wimpy.net.nih.gov (8.12.11/8.11.7) with ESMTP id j9BD75vn011071 for ; Tue, 11 Oct 2005 09:07:06 -0400 (EDT) Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by wimpy.net.nih.gov (8.12.11/8.11.7) with ESMTP id j9BD75E2011068; Tue, 11 Oct 2005 09:07:05 -0400 (EDT) User-Agent: Microsoft-Entourage/10.1.4.030702.0 Date: Tue, 11 Oct 2005 09:07:04 -0400 Subject: Re: question re. count, group by, and having From: Sean Davis To: Rick Schumeyer , Message-ID: In-Reply-To: <000b01c5ce62$514db090$0300a8c0@dell8200> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.013 required=5 tests=[AWL=0.013] X-Spam-Level: X-Archive-Number: 200510/85 X-Sequence-Number: 22938 On 10/11/05 8:50 AM, "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? I think that postgres is smart enough to do the evaluation only once, but this might be version-dependent, but one of the gurus will have to comment on which version (if there is a version dependence) first made this improvement. Sean