dbTalk Databases Forums  

Re: having clause question

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Re: having clause question in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ian Barwick
 
Posts: n/a

Default Re: having clause question - 10-30-2004 , 05:36 PM






On Sat, 30 Oct 2004 15:17:16 -0700, Shane Wegner
<shane-keyword-pgsql.a1e0d9 (AT) cm (DOT) nu> wrote:
Quote:
Hello,
(...)
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?
select lastname,count(*) as c from names group by lastname having count(*) > 1;

HTH

Ian Barwick
barwick (AT) gmail (DOT) com

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: having clause question - 10-30-2004 , 06:12 PM






Ian Barwick <barwick (AT) gmail (DOT) com> writes:
Quote:
On Sat, 30 Oct 2004 15:17:16 -0700, Shane Wegner
shane-keyword-pgsql.a1e0d9 (AT) cm (DOT) nu> wrote:
Under MySQL, this query does the trick.
select lastname,count(*) as c from names group by lastname
having c > 1;

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

select lastname,count(*) as c from names group by lastname having count(*) > 1;
Just to expand on that: MySQL's abbrevation is not legal SQL, and it's
not even very sensible, because logically speaking the SELECT output
list is only evaluated after (and if) the HAVING clause succeeds.
So it makes no sense for the HAVING clause to refer to SELECT values.

Postgres does optimize the case of multiple identical aggregate-function
invocations in a query, BTW, so the apparent inefficiency is not real.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.