deleting duplicate accounts between tables

  • I have been assigned a project where i must compare two tables of account information. Table one is our current account Database, Table two is a new account database. What I need to do is find out which accounts in Table two are not found in table one, in other words, i need to query which are the unique accounts between the two tables, i need all of the accounts found in Table two that are not found in table one. We would basically like to kick out all of the accounts in table two that also appear in table one so all accounts in table two are unique. Hope this makes sense.

  • This should do it.

    DELETE Database2.dbo.TABLE2

     FROM Database2.dbo.TABLE2 t2

     INNER JOIN Database1.dbo.TABLE1 t1

      ON t1.Account_number = t2.Account_number

    Steve

  • Hi,

    Try both and look for which is faster:

     DELETE FROM Table2 WHERE AccountNumber IN(Select AccountNumber from Table1)

      INSERT INTO Table1 from Table2

    --- Method 2

     INSERT INTO Table1 FROM Table2 WHERE AccountNumber NOT IN(select AccountNumber from Table1)

    Ahmed

  • You can create a new table of table 2 unique rows by a simple select statement:

    select *

    into table3

    from table2 t2 left outer join table1 t1

    on t2.KeyColumn=t1.KeyColumn

    where t1.KeyColumn is null

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply