Re: Beginner needs help - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Beginner needs help
Date
Msg-id 200303031018.10219.josh@agliodbs.com
Whole thread Raw
In response to Beginner needs help  (Aaron Chu <astrate@mac.com>)
List 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_tableGROUP BY surnameHAVING count(*) > 1     AND surname =
names_table.surname); 

--
Josh Berkus
Aglio Database Solutions
San Francisco


pgsql-sql by date:

Previous
From: Oliver Elphick
Date:
Subject: Re: Insert multiple Rows
Next
From: Joe Conway
Date:
Subject: Re: Querying Hierarchical Data