Re: How to get a count() where column < ''?

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Joost Kraaijeveld <J(dot)Kraaijeveld(at)Askesis(dot)nl>
Cc: "Pgsql-sql(at)postgresql(dot)org" <Pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to get a count() where column < ''?
Date: 2005-12-04 22:29:16
Message-ID: 20051204142347.R57404@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Joost Kraaijeveld 2005-12-04 22:36:50 Re: How to get a count() where column < ''?
Previous Message Jim Johannsen 2005-12-04 22:17:40 Re: How to get a count() where column < ''?