我有條不紊的瘋狂行不通......我錯過了一些東西。這是我第一次負責清理同桌中的重複部分。我用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的美麗視若無睹......但那將是一場悲劇。