查找重复记录sql的ID

时间:2014-07-08 14:48:17

标签: sql sql-server-2014

我有一个表有很多重复记录,即tbl_voter,ID为主键,自动增量。并且还有另一个表tbl_notes,其中包含每个选民的注释。 tbl_notes可以包含每个选民的零个或多个记录。来自tbl_voter的ID是tbl_notes中的外键。

问题是由于选民表中有重复,因此注释表中也有重复。

例如:tbl_voter

ID    Name    Address
01    abc     xyz
02    def     pqr
03    abc     xyz
04    abc     xyz
05    abc     xyz
06    def     pqr

tbl_notes

Noteid    ID     Note
A001      01     aaaaaa
A002      02     bbbbbb
A003      01     cccccc
A004      03     dddddd
A005      03     eeeeee
A006      04     ffffff
A007      05     gggggg
A008      01     hhhhhh

我想查找原始ID及其副本的所有ID,以便更新tbl_notes

例如:tbl_voter

ID    Name    Address
01    abc     xyz
02    def     pqr

tbl_notes

Noteid    ID     Note
A001      01     aaaaaa
A002      02     bbbbbb
A003      01     cccccc
A004      01     dddddd
A005      01     eeeeee
A006      01     ffffff
A007      01     gggggg
A008      01     hhhhhh

到目前为止,我一直试图找到重复的记录,但它给了我原始的和重复的记录。我需要一个回复我的查询:

RealID     DuplicateID
01         03
01         04
01         05
02         06

我试过的查询:

select *
from tbl_voter a inner join 
(
select id,firstname,lastname,zip,housenumber,COUNT(*) AS dupes  from tbl_voter
where riding = '35019'
group by
firstname,lastname,zip,housenumber
having count(*) > 1 
) b on a.firstname = b.firstname
and a.lastname = b.lastname
and a.zip = b.zip
and a.firstname is not null
and b.firstname is not null
and a.riding='35019'
and a.housenumber=b.housenumber
order by a.firstname asc

如果我在选择查询中添加ID,则会抛出一个错误,指出ID不能在查询中使用,这是正确的,因为ID会一直不同。

考虑了ID的查询:

select a.id as realid, b.id as dupid, a.firstname,a.lastname,a.zip,a.housenumber
from tbl_voter a inner join 
(
select id,firstname,lastname,zip,housenumber,COUNT(*) AS dupes  from tbl_voter
where riding = '35019'
group by
id,firstname,lastname,zip,housenumber
having count(*) > 1 
) b on a.firstname = b.firstname
and a.lastname = b.lastname
and a.zip = b.zip
and a.firstname is not null
and b.firstname is not null
and a.riding='35019'
and a.housenumber=b.housenumber
order by a.firstname asc

如果我得到重复的ID和真实的ID,我可以更新tbl_notes。

谢谢, 沙善

2 个答案:

答案 0 :(得分:5)

我只专注于找到具有真实id和重复id的表。在基本级别,您正在尝试查找信息重复的所有id对。基本上,我们可以从这个表开始:

RealID     DuplicateID
01         01
01         03
01         04
01         05
03         01
03         03
03         04
03         05
04         01
04         03
04         04
04         05
05         01
05         03
05         04
05         05
02         02
02         06
06         02
06         06

现在,这是比您想要的更多信息,但是像这样考虑您的表会大大简化查询。您想在tbl_voter上创建一个自连接,其中所有非id数据都匹配。

接下来,让我们过滤掉很多信息。只需指定realID必须小于DuplicateID,就可以删除大量不必要的信息。然后你的表看起来像这样:

RealID     DuplicateID
01         03
01         04
01         05
03         04
03         05
04         05
02         06

这仍然是过多的信息,但没有那么多。消除多余信息可以做的最后一件事是按duplicateID分组并选择min(realID)。这将为您提供您正在寻找的表格。该查询如下所示:

Select min(v.id) as RealID, v2.id as DuplicateId
From tbl_voter v join tbl_voter v2
    on v.firstname = v2.firstname
    and v.lastname = v2.lastname
    and v.zip = v2.zip
    and v.firstname is not null
    and v2.firstname is not null
    and v.riding='35019'
    and v.riding = v2.riding
    and v.housenumber=v2.housenumber
    and v.id < v2.id
Group by v2.id

答案 1 :(得分:0)

您可以尝试根据键列(而不是自动增量列)生成ROW_NUMBER / RANK,并选择所有row_number = 1的记录。像下面那样的东西

SELECT id,firstname,lastname,zip,housenumber from (SELECT id,firstname,lastname,zip,housenumber,ROW_NUMBER()OVER(PARTITION BY id,firstname,lastname,zip,housenumber ORDER BY id,firstname,lastname,zip,housenumber)as rowNumber FROM tbl_voter)WHERE rowNumber = 1 < / p>

相关问题