SQL query deletes duplicate data

Source: Internet
Author: User

Database UserInfo

Delete duplicate user name delete duplicate username phone number the same user name phone number only one user is reserved

01. Querying duplicate data based on multiple fields

With Data1 as (

Select Mobilephone,name from UserInfo
GROUP BY Mobilephone,name
Having Count (*) >1

),

02. Duplicate Data Allocation number

Data2 as (

Select U.*,row_number () over (partition by U.mobilephone,u.name ORDER by Id) RowNum from UserInfo u
INNER JOIN Data1 D on U.name=d.name and U.mobilephone=d.mobilephone

),

03, the query needs to delete the data primary key (each group number is one of the retention of the other delete)

Data3 as (

Select Id from Data2 where rownum>1

)

04. Delete data based on primary key

Delete from UserInfo
where Id in (select ID from data3)

After deletion

The use of partition function partition by in SQL Server http://www.cnblogs.com/zhangchengye/p/5473860.html

SQL query deletes duplicate data

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.