2012-06-04 59 views
3

我有一個叫做Member(唯一ID是MemberID)的表,它有許多成員重複,只有第一個和最後一個名字不同,但商業名稱,地址,城市,州和郵政編碼都是一樣。記錄導入重複。查找重複的條目SQL

如何運行腳本以查找BusinessName,Addr1,City,State和ZIP都相同的重複成員。

我想列在一個頁面上,所以我可以選擇哪些消除。

任何想法如何爲此創建腳本?

提前許多感謝,

保羅

回答

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確實不是匹配的副本時發生。這意味着如果有一個獨特的行,那麼將不會有結果,而如果有一行有一個或多個副本,那麼它將被返回。

+0

完美的作品..非常感謝..! – neojakey

+0

只是爲了完整性:這不會給你任何地址列中包含NULL的重複項。 –

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告訴你有多少重複的也有。