6 ways to share SQL Server delete duplicate rows _mssql

Source: Internet
Author: User
Tags repetition
1. If there is an ID field, it is a unique field
Copy Code code as follows:

Delect table where ID not in (
Select Max (ID) from table GROUP by col1,col2,col3 ...
)

The field followed by the GROUP BY clause is the condition that you use to determine the repetition, such as only col1, so as long as the content of the Col1 field is the same as that of the record.

2. If you're judging all the fields, you can do this.
Copy Code code as follows:

SELECT * into #aa from table group by Id1,id2,....
Delete Table
INSERT INTO table
SELECT * FROM #aa

3. Absence of ID
Copy Code code as follows:

Select Identity (int,1,1) as id,* into #temp from tabel
Delect # where ID not in (
Select Max (ID) from # Group by col1,col2,col3 ...)
Delect table
Inset into table (...)
Select ... from #temp

4. col1+ ', ' +col2+ ', ' ... col5 Union primary key
Copy Code code as follows:

SELECT * FROM table where col1+ ', ' +col2+ ', ' ... col5 in (
Select Max (col1+ ', ' +col2+ ', ' ... col5 ') from table
Where has count (*) >1
GROUP BY Col1,col2,col3,col4
)

The field followed by the GROUP BY clause is the condition that you use to determine the repetition, such as only col1, so as long as the content of the Col1 field is the same as that of the record.

5.
Copy Code code as follows:

Select Identity (int,1,1) as id,* into #temp from tabel
SELECT * from #temp where ID in (
Select Max (ID) from #emp where has count (*) >1 GROUP by col1,col2,col3 ...)

6.
Copy Code code as follows:

SELECT DISTINCT * into #temp tablename
Delete TableName
Go
Insert TableName SELECT * FROM #temp sqlclub
Go
drop table #temp

The above is a description of how SQL Server deletes duplicate rows.
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.