特定のフィールドで重複しているレコードに関するすべての情報を取得する必要があります。
mysql を使用する場合、次の方法で解決できます。
drop table if exists test;
create table test (
id int not null auto_increment primary key,
surname varchar(50),
firstname varchar(50),
sex char(1),
dob date,
pob varchar(50),
otherfield1 varchar(50),
otherfield2 varchar(50)
) engine = myisam;
insert into test (surname,firstname,sex,dob,pob,otherfield1,otherfield2)
values
('smith','john','M','2000-01-01','rome','xxx','yyy'),
('black','jack','M','1990-12-30','milan','aaaaa','vvvv'),
('smith','john','M','2000-01-01','rome','zzz','aaaaa'),
('white','mike','M','1980-03-01','naples','zzz','other text'),
('white','mike','M','1980-03-01','naples','zzz','foo bar'),
('smith','ann','F','1992-03-05','turin','aaaaaaa','other text');
select * from test where (surname,firstname,sex,dob,pob) in (
select
surname,firstname,sex,dob,pob
from test
group by surname,firstname,sex,dob,pob
having count(*) > 1
)
そして私は得る
"id" "surname" "firstname" "sex" "dob" "pob" "otherfield1" "otherfield2"
"1" "smith" "john" "M" "2000-01-01" "rome" "xxx" "yyy"
"3" "smith" "john" "M" "2000-01-01" "rome" "zzz" "aaaaa"
"4" "white" "mike" "M" "1980-03-01" "naples" "zzz" "other text"
"5" "white" "mike" "M" "1980-03-01" "naples" "zzz" "foo bar"
ただし、この方法は mssql 2005 では機能しません。
create table #test (
id int identity,
surname varchar(50),
firstname varchar(50),
sex char(1),
dob datetime,
pob varchar(50),
otherfield1 varchar(50),
otherfield2 varchar(50)
)
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('smith','john','M','2000-01-01','rome','xxx','yyy');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('black','jack','M','1990-12-30','milan','aaaaa','vvvv');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('smith','john','M','2000-01-01','rome','zzz','aaaaa');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('white','mike','M','1980-03-01','naples','zzz','other text');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('white','mike','M','1980-03-01','naples','zzz','foo bar');
insert into #test (surname,firstname,sex,dob,pob,otherfield1,otherfield2) values ('smith','ann','F','1992-03-05','turin','aaaaaaa','other text');
select * from #test where (surname,firstname,sex,dob,pob) in (
select
surname,firstname,sex,dob,pob
from #test
group by surname,firstname,sex,dob,pob
having count(*) > 1
)
前もって感謝します。
編集
これは私が見つけた可能な解決策です:
select t1.* from #test as t1
inner join (select
surname,firstname,sex,dob,pob
from #test
group by surname,firstname,sex,dob,pob
having count(*) > 1) as t2
on t1.surname = t2.surname and t1.firstname = t2.firstname and t1.sex = t2.sex and t1.dob = t2.dob and t1.pob = t2.pob
しかし、もっと良い方法があるかどうか知りたいです。これらすべての条件に参加するのは好きではありません。