X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 0E98E9DCBAA for ; Sun, 4 Dec 2005 18:29:18 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 33073-08 for ; Sun, 4 Dec 2005 18:29:14 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id ECAD79DCA9F for ; Sun, 4 Dec 2005 18:29:15 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 6F0F2350E5; Sun, 4 Dec 2005 14:29:16 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 6CFD3350E4; Sun, 4 Dec 2005 14:29:16 -0800 (PST) Date: Sun, 4 Dec 2005 14:29:16 -0800 (PST) From: Stephan Szabo To: Joost Kraaijeveld Cc: "Pgsql-sql@postgresql.org" Subject: Re: How to get a count() where column < ''? In-Reply-To: <1133732596.5560.11.camel@Panoramix> Message-ID: <20051204142347.R57404@megazone.bigpanda.com> References: <1133730772.5560.8.camel@Panoramix> <20051204133233.O57404@megazone.bigpanda.com> <1133732596.5560.11.camel@Panoramix> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/22 X-Sequence-Number: 23564 On Sun, 4 Dec 2005, Joost Kraaijeveld wrote: > Hi Stephan, > > On Sun, 2005-12-04 at 13:33 -0800, Stephan Szabo wrote: > > > SELECT COUNT(customers.objectid) FROM prototype.customers, > > > prototype.addresses > > > WHERE > > > customers.contactaddress = addresses.objectid > > > AND > > > zipCode < '2716BN' > > > ORDER By zipCode, houseNumber > > > In a non-grouped query like the above, I don't think that the order by is > > meaningful. You only get one row back anyway without a group by, and > > there's no single zipCode or houseNumber to associate with the row. > > > What do you mean by a non-grouped query? The query below gives the same > error: A query without a group by, in other words one on which the count is done over the entire set of rows that pass the where clause. > SELECT zipcode, COUNT(*) FROM prototype.customers, prototype.addresses > WHERE > customers.contactaddress = addresses.objectid > AND > zipCode < '2716BN' Yes, because without a group by there's one count and it has no associated zipcode to put in the select list. I believe select count(*) from prototype.customers, prototype.addresses where customers.contactaddress = addresses.objectid and zipCode < '2716BN'; will work and give you an overall count. select zipcode, count(*) from prototype.customers, prototype.addresses where customers.contactaddress = addresses.objectid and zipCode < '2716BN' group by zipcode order by zipcode; should give you a list broken up with a count by zipcode in order of zipcode.