2013-02-15 19 views
-3

我有條不紊的瘋狂行不通......我錯過了一些東西。這是我第一次負責清理同桌中的重複部分。我用google搜索了很多東西,比如用普通的表格表達式刪除等等,但是沒有什麼可以使用的。在公共表格表達式和相關更新中在同一表格中標記重複的數據

我的地址表是這樣的:

Address 
-------- 
id 
add1 
add2 
city 
state 
zip 
parentidofthisdup  

我想重複和行號。我認爲行號1的id是父母。爲了避免後續行的重複地址,我想將這些父母的身份標記在arentidofthisdup上。我最終會保留父母並處理父母身份在父母身份中的父母。

我想通過做一個公用表表達式,然後在相關性更新中使用cte來做這個更新,但是yikes,這不起作用。我得到的是所有記錄都是更新的,但只有空值導致parentidofthisdup。

也許我沒有用正確的方式編碼。我在大規模更新方面相當新穎。

-- My common table expression of the set that I want stamped 
with tbFlagTheseWithPk as 
(
Select * from 

(
select 
    myaddress.id, 
    myaddress.parentidofthisdup,    
    myaddress.add1, 
    myaddress.add2, 
    myaddress.state, 
    myaddress.zip, 
    row_number() over (partition by add1, state, zip order by add1, state, zip, add2) as [rn] 
    from myaddress 
    where  add1 !='' 
) as a 
where a.rn > 1) 

-- Now use our Common Table Expression using a correlated subquery to make them children of rn 1 

Update tbFlagTheseWithPk 
set 
set parentidofthisdup = 
( Select id from                         
    (Select * from 
    ( select  myaddress.pkey,                   myaddress.parentidofthisdup,                 myaddress.add1,                     myaddress.add2, 
myaddress.state, 
myaddress.zip, 
row_number() over (partition by add1, state, zip order by a1, state, zip, add2) as [rn] 
from myaddress where add1 !='' 
    ) as a                         
    where a.rn > 1) as b 

    where b.a1 = tbFlagTheseWithPk.add1                     
    and 
b.state = tbFlagTheseWithPk.state 
and 
b.zip = tbFlagTheseWithPk.zip 

    and 
tbFlagTheseWithPk.rn = 1 

難道沒有更好的方法嗎?我如何克服這個大規模更新學習曲線?我覺得我應該能夠以一種優雅的方式來做到這一點,但如果我不盡快弄清楚這一點,我將會求助於循環遊標並對SQL的美麗視若無睹......但那將是一場悲劇。

回答

1

切勿使用遊標。

你在正確的軌道上。 這些鏈接可以幫助 SQL Server - inner join when updating ,ROW_NUMBER http://msdn.microsoft.com/en-us/library/ms186734.aspx ,CTE http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

DECLARE @myAddress table 
(id int, parentidofthisdup int, add1 nvarchar(10),add2 nvarchar(10) , [state] nvarchar(10),zip nvarchar(10)) ; 


Insert into @myAddress Values(1,null,'a','b','c','d'); 
Insert into @myAddress Values(2,null,'a','b','c','d'); 
Insert into @myAddress Values(3,null,'a','b','c','d'); 
Insert into @myAddress Values(5,null,'a','b','c','d'); 
Insert into @myAddress Values(6,null,'a','f','c','d'); 
Insert into @myAddress Values(7,null,'a','b','g','d'); 
Insert into @myAddress Values(8,null,'a','f','c','d'); 
with cte AS 
(
select 
    myaddress.id, 
    myaddress.parentidofthisdup,    
    myaddress.add1, 
    myaddress.add2, 
    myaddress.state, 
    myaddress.zip, 
    row_number() over (partition by add1, add2, state, zip order by id,add1, [state], zip, add2) as [rn] 
    from @myaddress myaddress 


) 


update r SET parentidOfthisDup 
    = cte.id 
    From cte Inner join @myAddress r 
    ON cte.add1 = r.add1 
      AND cte.add2 =r.add2 
      AND cte.Zip =r.zip 
      AND cte.[state] =r.[state] 
      and cte.id<>r.id 
    WHERE cte.rn = 1 


select * from @myAddress 
相關問題