Use Row_number () in SQL Server 2005 to delete duplicate records in a single query _mssql2005

Source: Internet
Author: User
Let's take a look at how you can use it to delete duplicate records in a table:
Copy Code code as follows:

If Exists (Select * FROM Tempdb.Information_Schema.Tables Where table_name like ' #Temp% ')
Drop Table #temp
Create Table #temp ([Id] int, [Name] varchar, [age] int, [SEX] bit default 1)
Go
Insert into #temp ([Id], [Name], [age], [Sex]) Values (1, ' James ', 25,default)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (1, ' James ', 25,default)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (1, ' James ', 25,default)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (2, ' Lisa ', 24,0)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (2, ' Lisa ', 24,0)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (2, ' Lisa ', 24,0)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (3, ' Mirsa ', 23,0)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (3, ' Mirsa ', 23,0)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (3, ' Mirsa ', 23,0)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (4, ' John ', 26,default)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (5, ' Abraham ', 28,default)
Insert into #temp ([Id], [Name], [age], [Sex]) Values (6, ' Lincoln ', 30,default)
Delete T from
(Select row_number () over (Partition by [id],[name],[age],[sex] ORDER by [ID]) as rownumber,* from #Temp) T
Where t.rownumber > 1
Select * from #temp

Note the penultimate script, which we implement in a query.
You can perform your own T-SQL script to see the effect. Hope to help you develop!
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.