How can I delete duplicate rows
where no unique row id
exists?
My table is
col1 col2 col3 col4 col5 col6 col7
john 1 1 1 1 1 1
john 1 1 1 1 1 1
sally 2 2 2 2 2 2
sally 2 2 2 2 2 2
I want to be left with the following after the duplicate removal:
john 1 1 1 1 1 1
sally 2 2 2 2 2 2
I've tried a few queries but i think they depend on a row id as I don't get desired result. For example:
DELETE FROM table WHERE col1 IN (
SELECT id FROM table GROUP BY id HAVING ( COUNT(col1) > 1 )
)
If you have no references, like foreign keys, you can do this. I do it a lot when testing proofs of concept and the test data gets duplicated.
SELECT DISTINCT [col1],[col2],[col3],[col4],[col5],[col6],[col7]
INTO [newTable]
;
Go into the object explorer and delete the old table.
Rename the new table with the old table's name.
If you have the ability to add a column to the table temporarily, this was a solution that worked for me:
Then perform a DELETE using a combination of MIN and GROUP BY
Verify that the DELETE performed correctly:
The result should have no rows with a count greater than 1. Finally, remove the rowid column: