sql server - Remove Duplicates ... With NULLs -
in ms sql server trying remove duplicates table nulls. que groans. lots , lots of null
s. bottom line need retain 1 copy of duplicate record or without null
s. wantnull
act normal record value of "null
" duration of operation, , go being real null
. possible? there simpler solution?
table1
looks like:
uid data1 data2 1 null 2 null 3 b abc 4 b abc 5 c null 6 d ghj
i want command throw away line 2 , 4 , keep rest. (the select testing.)
;select uid, data1, data2 table1 t not exists ( select 1 table1 t2 t2.data1 = t.data1 , t2.data2 = t.data2 , t2.uid >= t.uid ) , data1 not null
note: select distinct not work duplicates have different timestamps.
this should do:
;with cte ( select *, rn = row_number() over(partition data1,data2 order uid) table1 ) delete --select * cte rn > 1
updated following comment
ok, if having issues delete amount of rows, could try creating table id's want delete , batch delete (you have test batch row amount, though). idea (assuming uid
pk):
;with cte ( select *, rn = row_number() over(partition data1,data2 order uid) table1 ) select [uid] rowstodelete cte rn > 1; create index i_uid on rowstodelete([uid]); while 1=1 begin delete top (10000) table1 t inner join rowstodelete l on t.[uid] = l.[uid] if @@rowcount < 10000 break; end
Comments
Post a Comment