查找重复条目SQL

3
我有一个名为Member(唯一ID是MemberID)的表格,其中有许多成员重复,只有名字和姓氏不同,但商业名称、地址、城市、州和邮政编码都相同。记录被重复导入。
我该如何运行一个脚本来查找重复的成员,其中BusinessName、Addr1、City、State和ZIP都相同。
我想将它们全部列在一个页面上,这样我就可以选择要删除哪些。
有什么办法可以为此创建一个脚本吗?
非常感谢您的帮助,
Paul
2个回答

1
select * from Member as m
where exists(select MemberID 
      from Member as m2 
      where 
           (m.BusinessName = m2.BusinessName or (m.BusinessName is null and m2.BusinessName is null)) and 
           (m.Addr1 = m2.Addr1 or (m.Addr1 is null and m2.Addr1 is null)) and 
           (m.City = m2.City or (m.City is null and m2.City is null)) and 
           (m.State = m2.State or (m.State  is null and m2.State is null)) and 
           (m.ZIP = m2.ZIP or (m.ZIP  is null and m2.ZIP is null)) and 
           m.memberID <> m2.MemberID)

通过上述查询,where 子句检查是否存在重复条目。如果子查询返回结果,则仅在 MemberID 不匹配的情况下才会返回结果。这意味着如果存在唯一行,则不会返回结果,而如果存在一行或多行副本,则将返回结果。

仅供完整性参考:这不会给您重复项,其中任何地址列包含NULL。 - Steve Kass

1
您想要使用分析函数来实现此功能:
select m.*
from (select m.*,
             count(*) over (partition by BusinessName, Address, City, State, ZipCode) as NumDups 
      from members m
     ) m
where NumDups > 1

NumDups告诉您有多少个重复项。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接