我有一个名为“成员(member)”的表(唯一 ID 为 MemberID),其中有许多重复的成员(member),只有名字和姓氏不同,但公司名称、地址、城市、州和邮政编码都相同。导入的记录有重复项。
如何运行脚本来查找 BusinessName、Addr1、City、State 和 ZIP 都相同的重复成员。
我想将它们全部列出在一个页面上,这样我就可以选择要删除哪些。
有什么想法可以为此创建一个脚本吗?
提前非常感谢,
保罗
最佳答案
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
不匹配的副本时,子查询才会返回结果。这意味着如果存在唯一的行,则不会有结果,而如果存在具有一个或多个副本的行,则将返回该行。
关于sql - 查找重复条目 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10885418/