Re: Beginner needs help

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Aaron Chu <astrate(at)mac(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Beginner needs help
Date: 2003-03-03 18:18:10
Message-ID: 200303031018.10219.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Aaron,

Thanks for an easy one:

> I have a table which has a column of surnames (string) and I would like
> to know how can I retrieve (SELECT) all the repeated surnames, i.e.
> more than one person who has the same surname.

To just get a list of repeated surnames:

SELECT surname, count(*) as names_count
FROM names_table
GROUP BY surname
HAVING count(*) > 1

To get the records these are attached to, call the above as a subquery:

SELECT names_table.*
FROM names_table
WHERE EXISTS ( SELECT surname, count(*)
FROM names_table
GROUP BY surname
HAVING count(*) > 1
AND surname = names_table.surname );

--
Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Erwin Moller 2003-03-03 18:24:15 Re: HardCORE QUERY HELP!!!
Previous Message Oliver Elphick 2003-03-03 17:52:41 Re: Insert multiple Rows