sql - delete records from 2 tables after testing to see if they are duplicates -


editing else's sql server design here:

i have been tasked removing duplicate rows database.

i have 2 tables: table1 , table2.

  • table1 has columns t1id, , t1name
  • table2 has columns t2id, data1, data2, data3

  • tables join on t1id = t2id

if several rows have same t1name, data1, data2, and data3, need remove 1 of them both tables.

i guess t1id in table1 , t2id in table2 primary key corrosponding tables.

if can use following approach -

1) want delete both tables, need store t1id (or t2id) 1 temp table can use values deleting second table

2) extracting desired t1id/t2id need group joined table t1name, data1, data2, data3 , select record getting multiple records.

3) exclude 1 t1id duplicate records found delete.

so can use commands shown below -

create table test_table  (select t1id       table1, table2      t1name, data1, data2,      data3 in (select t1name, data1, data2, data3                        (select t1name, data1, data2, data3, count(*)                                table1, table2                               t1id = t2id                               group t1name, data1, data2, data3                              having count(*) > 1))        , t1id not in (select min(t1id)                           table1, table2                          t1id = t2id                          group t1name, data1, data2, data3));  delete table1 t1id in (select t1id test_table);  delete table2 t2id in (select t1id test_table);  commit;  drop table test_table; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -