3

我有一个名为 Member(唯一 ID 是 MemberID)的表,其中有许多重复的成员,只有名字和姓氏不同,但公司名称、地址、城市、州和邮政编码都是相同的。导入的记录有重复。

如何运行脚本来查找 BusinessName、Addr1、City、State 和 ZIP 都相同的重复成员。

我想将它们全部列出在一个页面上,这样我就可以选择要删除的那些。

有什么想法可以为此创建脚本吗?

提前谢谢了,

保罗

4

2 回答 2

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 告诉您有多少重复项。

于 2012-06-04T17:28:38.083 回答
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返回结果这意味着如果有一个唯一的行,那么将没有结果,而如果有一个具有一个或多个副本的行,那么它将被返回。

于 2012-06-04T17:30:01.007 回答