Friday 5 January 2018

delete duplicate row and update reference in child table

--updates the data table to the min ids for each name
update t2
set Id = final_id
from
  t2
join
  t1
on t1.d = t2.Id
join
(
  select
    Name,
    min(Id) as final_id
  from t1
  group by Name
) min_ids
on min_ids.name = t1.name

--deletes redundant ids from the t1 table
delete
from t1
where Id not in
(
  select
    min(id) as final_id
  from t1
  group by name
)

No comments:

Post a Comment