1

特定のフィールドで重複しているレコードに関するすべての情報を取得する必要があります。

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

しかし、もっと良い方法があるかどうか知りたいです。これらすべての条件に参加するのは好きではありません。

4

2 に答える 2

1
SELECT * /*TODO: Just list desired columns*/
FROM   (SELECT *,
                Count(*) OVER (PARTITION BY surname,firstname,sex,dob,pob) AS Cnt
        FROM   #test) T
WHERE  Cnt > 1 

-または

SELECT *
FROM   #test t1
WHERE  EXISTS (SELECT *
               FROM   #test t2
               WHERE  t1.id <> t2.id
                      AND EXISTS (SELECT t1.surname,
                                         t1.firstname,
                                         t1.sex,
                                         t1.dob,
                                         t1.pob
                                  INTERSECT
                                  SELECT t2.surname,
                                         t2.firstname,
                                         t2.sex,
                                         t2.dob,
                                         t2.pob)) 
于 2012-07-01T14:29:39.123 に答える
0
select surname,firstname,sex,dob,pob from 
(
select  
surname,firstname,sex,dob,pob, count(*) as NumberOfRecords
from #test 
group by surname,firstname,sex,dob,pob 
) dupCount
Where dupCount.NumberOfRecords > 1

これは2005年に機能するはずです

于 2012-07-01T12:33:31.530 に答える