Sqlserver Delete duplicate data

Source: Internet
Author: User

I. Filtering duplicate data

1. Completely Repeated Records

 
Select DistinctField 1, Field 2, Field 3FromData Table

2. Record with duplicate key fields

 
/*Data Structure: Role file (role encoding, role, and role classification encoding) function: retrieve the non-duplicated data with the specified field (role classification encoding) as the keyword. The first repeated description is as follows: repeat the record to get the last one. You only need to change min to Max.*/ Select * FromRole file tWhereRole codeIn(Select Min(Role code)FromRole file T1Group ByT1. role classification code)

 

Ii. Delete duplicate records

During database usageProgramThis section describes how to delete duplicate data.

 

There are two Repeated Records. One is a completely repeated record, that is, records with all fields being repeated, and the other is records with duplicate key fields, such as duplicate name fields, other fields are not necessarily repeated or can be ignored.

1. For the first type of repetition, it is easier to solve.

 
Select Distinct * FromTablename

 

You can get the result set without repeated records.

If the table needs to delete duplicate records (one record is retained), you can delete the record as follows:

Select Distinct * Into# TMPFromTablenameDrop TableTablenameSelect * IntoTablenameFrom# TMPDrop Table# TMP

 

The reason for this repetition is that the table design is not weekly. You can add a unique index column.

2. Repeat problems usually require that the first record in the repeat record be retained. The procedure is as follows:

Assume that the duplicate fields are Name and address. You must obtain the unique result set of the two fields.

 Select  Identity ( Int , 1 , 1 ) As Autoid, *   Into # TMP From  Tablename  Select   Min (Autoid) As Autoid Into # Tmp2 From # TMP Group   By  Name, autoid  Select   *   From # TMP Where Autoid In ( Select Autoid From # Tmp2)

 

The last SELECT command gets the result set with no duplicate name and address (but an autoid field is added, which can be omitted in the select clause when writing)

 

My demo:

 Select   Identity (Int , 1 , 1 ) As  Autoid,  [  Productcode  ] , [  Categoryid  ]   Into # TMP From  DBO. tblproduct  Select  Min (Autoid) As Autoid Into # Tmp2 From # TMP Group   By  Productcode, companyid  Truncate   Table  DBO. tblproduct  Insert   Into Tblproduct ( [  Productcode  ] ,[  Categoryid  ]  )  Select   [  Productcode  ] , [  Categoryid  ]         From  # TMP  Where Autoid In ( Select AutoidFrom  # Tmp2)  Select   Count ( * ) From  # TMP  Select   Count ( * ) From  # Tmp2  Drop   Table  # TMP  Drop  Table # Tmp2

 

 

 

Original article: http://fdsazi.blog.51cto.com/1871366/375949/

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.