A few days ago, I read the article in the SQL section of SCID and explained how to quickly delete duplicate records in SQL Server. I browsed it. The author used four methods: creating temporary tables, using cursors, and using unique indexes. After a while, I found that the method I used was the easiest. Good stuff cannot be exclusive...
The data in the test table is as follows:
ProductID |
ProductName |
Unit |
UnitPrice |
1 |
North |
Bottole |
9.0 |
2 |
Chang |
Bottole |
20.0 |
2 |
Chang |
Bottole |
20.0 |
3 |
Anistreed |
G |
1.0 |
4 |
Thu |
Kg |
2.8 |
4 |
Thu |
Kg |
2.8 |
4 |
Thu |
Kg |
2.8 |
The records of product Chang and Tofu are repeated in the product information table. To delete these duplicate records, only one of them is retained.
Purpose: To delete duplicate rows in a table. Only one record is left for each Product. Assume that the table name is TEST.
Step 1: add an ID column for the original table.
Alter Table TEST Add Id int Identity (1, 1)
In this way, the table data is processed as follows:
ID |
ProductID |
ProductName |
Unit |
UnitPrice |
1 |
1 |
North |
Bottole |
9.0 |
2 |
2 |
Chang |
Bottole |
20.0 |
3 |
2 |
Chang |
Bottole |
20.0 |
4 |
3 |
Anistreed |
G |
1.0 |
5 |
4 |
Thu |
Kg |
2.8 |
6 |
4 |
Thu |
Kg |
2.8 |
7 |
4 |
Thu |
Kg |
2.8 |
Step 2: run the following SQL statement.
Delete from TEST Where ID Not In (Select MIN (ID) From TEST Group by ProdutyID)