SQL服务器重复,结果为XY,YX重复

时间:2015-02-16 10:03:56

标签: sql duplicates distinct multiple-columns

我的表格包含一些公司的信息。该表包含此列。

Connectionkey | Phonenumber来|姓氏|姓| Categoryid1 | housenumber | Streettype |街头|邮编|局部性| Createts | lastuserid

我正在尝试根据某些列提取重复的行

SELECT
Dup.Connectionkey,
Dup.Phonenumber,
Dup.Lastname,
Dup.Firstname,
Dup.Categoryid1,
Dup.housenumber,
Dup.Streettype,
Dup.Street,
Dup.Zipcode,
Dup.Locality,
Dup.Createts,
Dup.lastuserid,
Dup1.connectionkey,
Dup1.Phonenumber,
Dup1.Lastname,
Dup1.Firstname,
Dup1.Categoryid1,
Dup1.housenumber,
Dup1.Streettype,
Dup1.Street,
Dup1.Zipcode,
Dup1.Locality,
Dup1.Createts,
Dup1.lastuserid
From 
spp Dup, spp Dup1
Where
Dup.supplierid in (4,6,7,9,11,16,48,49,50,51,52,53,54,55,56,61,62,63,65,66,67,68,70,74,77,78,79,80,87,84)
And 
Dup1.supplierid in (4,6,7,9,11,16,48,49,50,51,52,53,54,55,56,61,62,63,65,66,67,68,70,74,77,78,79,80,87,84)
And 
Dup.supplierid <> Dup1.supplierid
And
Dup.phonenumber=Dup1.phonenumber
And 
Dup.zipcode=Dup1.zipcode
And
Dup.Locality=Dup1.Locality
And
(
Dup.street like '%'+Dup1.street+ '%'
Or
Dup1.street like '%'+Dup.street+ '%'
)
And
(
Dup.lastname like '%'+Dup1.lastname+ '%'
Or
Dup1.lastname like '%'+Dup.lastname+ '%'
)

我对查询的问题是,来自&#34; Dup&#34;的结果数据在&#34; Dup2&#34;中得到重复。 所以在结果中我有XY和YX。这是相同的。

我的问题是:我如何避免这种重复,我使用了DISTINCT而且它没有用。

1 个答案:

答案 0 :(得分:1)

你想改变......

Dup.supplierid <> Dup1.supplierid

为...

Dup.supplierid < Dup1.supplierid

现在你只会得到每一对!