I have a table which was imported from MS-SQL, it has about 1.9 million rows. After importing I realized that a lot of the rows are duplicates and there are no primary key constraints.

The rules to check for constraints are:

  • Check the email address.
  • If no duplicate is found in the table, leave the row as it is.
  • If email address matches then compare the columns in both.
  • Select one of the two as the row that will be kept i.e. TO_KEEP, the other will be deleted i.e. TO_DELETE.
  • If there is any column in TO_DELETE that has some value but there is no value in TO_KEEP the update the TO_KEEP row with the value from the matching column in TO_DELETE.

I need help with writing a query for that. It's beyond my current skills. I wouldn't mind if I get some pointers which can make it easier as to how the query can be structured and which ones to use.



Browse other questions tagged or ask your own question.