2016-04-03 73 views
0

我有一個包含許多記錄的表,其中有一個nic列應具有唯一值。但它包含一些污垢,意味着那裏有一些重複的值。糾正SQL Server數據庫中的重複記錄

像與

id = 567 has nic = '786-786' 

id = 897 has nic = '786-786'. 

我想寫一個存儲過程我應該在最後刪除那些重複的和追加1像

id = 567 , nic= '786-786' 
id = 788 , nic = '786-786-1' 
id = 2344, nic = '786-786-2' 

是用戶有可能嗎?

+1

標籤mysql ...你需要什麼數據庫引擎查詢,MS SQL或MySQL? –

+0

它是'MSSQL引擎'。 –

回答

0

使用窗口功能/分析功能,如

Row_number() over (partition by Nic, order by ID)

SQL Fiddle 喜歡的東西...

UPDATE B 
SET B.Nic = C.NewNic 
FROM Foo B 
INNER JOIN (SELECT ID, NIC + '-' + cast(Row_number() 
over (partition by NIC order by ID) as varchar(20)) as newNic 
FROM FOo) C 
on C.ID = B.ID 

這會給你

id = 567 , nic= '786-786-1' 
id = 788 , nic = '786-786-2' 
id = 2344, nic = '786-786-3' 

你總是能做個案陳述和當行1,則不會顯示再減去1,如果你要離開,沒有一個-1

SQL Fiddle getting rid of the -1 for first record...

UPDATE B 
SET B.Nic = Case WHEN C.RN = 1 then B.Nic Else B.Nic+ '-'+cast((RN-1) as varchar(20)) end 
FROM Foo B 
INNER JOIN (SELECT ID, NIC, cast(Row_number() 
over (partition by NIC order by ID) as varchar(20)) as RN 
FROM FOo) C 
on C.ID = B.ID 

的第一個結果造成:

ID NIC 
567 786-786 
897 786-786-1 

SQL Fiddle顯示多個網卡

ID NIC 
567 786-786 
600 786-786-1 
897 786-786-2 
10 786-787 
12 786-787-1 
900 786-787-2 
+0

無法正常工作,在'Inner join'第2行和第3行'as'處出現語法錯誤。 –

+0

提供了更新和工作的SQL小提琴。 – xQbert

0

for tsql

您可以通過簡單的更新做到這一點,試試這個查詢:

select id, nic, rank() over (partition by nic order by id) 
from your_table as t1 
where exists(select 1 from your_table where nic = t1.nic and id <> t1.id) 

您可以構建更新查詢,或返回更新的字符串作爲這個選擇的領域。閱讀關於rank()