having clause question

From: Shane Wegner <shane-keyword-pgsql(dot)a1e0d9(at)cm(dot)nu>
To: pgsql-general(at)postgresql(dot)org
Subject: having clause question
Date: 2004-10-30 22:17:16
Message-ID: 20041030221716.GA1130@cm.nu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I am attempting to convert a query from MySQL to PostgreSQL
and am having some trouble using an aggregate column in a
having clause. As a quick example, take the following SQL
script:

create table names(
firstname varchar(40) not null,
lastname varchar(40) not null
);

insert into names values('John','Smith');
insert into names values('Bob','Smith');
insert into names values('Mike','Skinner');

I want to retrieve any last names with more than 1
occurence in the table. Under MySQL, this query does the
trick.
select lastname,count(*) as c from names group by lastname
having c > 1;

But under PG, it errors out
ERROR: column "c" does not exist

Is it possible to do a query like this with PostgreSQL?

Thanks,
Shane

--
Shane Wegner
http://www.cm.nu/~shane/

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ian Barwick 2004-10-30 22:24:43 Re: counting records of schema
Previous Message Jan Wieck 2004-10-30 20:15:07 Re: The reasoning behind having several features outside