SQL deletes duplicate and SQL duplicate records in a table.

Source: Internet
Author: User

SQL deletes duplicate and SQL duplicate records in a table.

I have encountered the problem of Repeated Records in the table. The fastest way to directly write SQL statements to delete the records is not to copy them to the excel table slowly for manual searching.

The following SQL statement finds duplicate records and records with the smallest ID value in Repeated Records (IDs in the table are self-increasing)

Select MIN (ID) as id, StructSN, Date, UserID, StarCount, COUNT (StructSN) as cfrom T_Dor_StructStar where Date> = '2013' group by StructSN, Date, UserID, starCounthaving COUNT (StructSN)> 1

Then you can directly Delete the record. The basic principle is to find each record of the record with the minimum duplicate id, and delete the remaining record.

Delete from T_Dor_StructStarwhere ID in (select s. ID from T_Dor_StructStar s, (select MIN (ID) as id, StructSN, Date, UserID, StarCount, COUNT (StructSN) as cfrom T_Dor_StructStar where Date> = '20140901' group by StructSN, date, UserID, StarCounthaving COUNT (StructSN)> 1) awherea. date = s. dateand. structSN = s. structSNand. userID = s. userIDand. starCount = s. starCountand. id! = S. ID)

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.