Beginner needs help

Поиск
Список
Период
Сортировка
Искать

Beginner needs help

От:
Aaron Chu <astrate@mac.com>
Дата:
Hi,

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.

Thanks.


Re: Beginner needs help

От:
Greg Stark <gsstark@mit.edu>
Дата:

Aaron Chu  writes:

> 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.

SELECT surname FROM table GROUP BY surname HAVING count(*) > 1

More generally, read up on GROUP BY.

-- 
greg


Re: Beginner needs help

От:
"Victor Yegorov" <viy@pirmabanka.lv>
Дата:
* Aaron Chu  [01.03.2003 03:22]:
> Hi,
> 
> 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.
> 
> Thanks.

Say you have such a table:
create table person (   name    varchar(10) not null,   surname varchar(20) not null
);       
Now your select:
selectsurname
fromperson
group by surname
having count(surname) > 1;               

-- 

Victor Yegorov

Re: Beginner needs help

От:
"Chad Thompson" <chad@weblinkservices.com>
Дата:
So, if I understand.  You want a list of people whose surnames appear more
than once?

Something like

select count(*), surname
from mytable
group by surname
having count(*) > 1

This will show a unique list of all the surnames that appear more than 1
time.

HTH
Chad
----- Original Message -----
From: "Aaron Chu" 
To: 
Sent: Thursday, February 27, 2003 4:17 PM
Subject: [SQL] Beginner needs help


> Hi,
>
> 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.
>
> Thanks.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>


Re: Beginner needs help

От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Thu, Feb 27, 2003 at 23:17:06 +0000, Aaron Chu  wrote:
> Hi,
> 
> 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.

select surname from whatevertable group by surname having count(*) > 1 order by surname;

Re: Beginner needs help

От:
Tomasz Myrta <jasiek@klaster.net>
Дата:
Aaron Chu wrote:>Hi,>>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.
>Thanks.

What exactly you want to do? To eliminate duplicates? Use then:
select surname from users group by surname;

Maybe you want to know which surnames are duplicated? Use then:
select surname from users group by surname having count(*)>1

Regards,
Tomasz Myrta



Re: Beginner needs help

От:
Josh Berkus <josh@agliodbs.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_tableGROUP BY surnameHAVING count(*) > 1 	AND surname = names_table.surname );

-- 
Josh Berkus
Aglio Database Solutions
San Francisco

Re: Beginner needs help

От:
Lee Harr <missive@frontiernet.net>
Дата:
In article , Björn Lundin wrote:
> Aaron Chu wrote:
> 
>> Hi,
>> 
>> 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.
> 
> select surname, count('a') from table
> group by surname
> having count('a') > 1
> order by surname
> 


SELECT DISTINCT surname FROM table t1, table t2 WHERE t1.surname = t2.surname   AND t1.oid != t2.oid
ORDER BY surname;


Re: Beginner needs help

От:
Björn Lundin <bjorn.lundin.no.spam@swipnet.se>
Дата:
Aaron Chu wrote:

> Hi,
> 
> 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.

select surname, count('a') from table
group by surname
having count('a') > 1
order by surname

/Björn 

FAQ