Quickly delete duplicate records in SQL Server

Source: Internet
Author: User

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)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.