Re: Beginner needs help

Поиск
Список
Период
Сортировка
От Josh Berkus
Тема Re: Beginner needs help
Дата
Msg-id 200303031018.10219.josh@agliodbs.com
обсуждение исходный текст
Ответ на Beginner needs help  (Aaron Chu <astrate@mac.com>)
Список 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 по дате отправления:

Предыдущее
От: Oliver Elphick
Дата:
Сообщение: Re: Insert multiple Rows
Следующее
От: Joe Conway
Дата:
Сообщение: Re: Querying Hierarchical Data