From: Josh Berkus <josh@agliodbs.com>
To: Aaron Chu <astrate@mac.com>
To: pgsql-sql@postgresql.org
Subject: Re: Beginner needs help
Date: Mon, 3 Mar 2003 10:18:10 -0800
Message-ID: <200303031018.10219.josh@agliodbs.com> (raw)
In-Reply-To: <96853212-4AA9-11D7-8F7C-000393017462@mac.com>
References: <96853212-4AA9-11D7-8F7C-000393017462@mac.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: josh@agliodbs.com, astrate@mac.com
Subject: Re: Beginner needs help
In-Reply-To: <200303031018.10219.josh@agliodbs.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox