In SQL Server database operations, sometimes the result set in a table satisfies certain rules we consider to be duplicate data, and these duplicate data need to be deleted. How do I delete it? This article is illustrated by an example.
Examples are as follows:
As long as the companyname,invoicenumber,customernumber are the same, we think it is duplicate data, the following example shows how to delete.
Declare @InvoiceListMaster table (ID int Identity primary key, CompanyName Nchar (), InvoiceNumber int, Cu Stomernumber int, rmanumber int) Insert @InvoiceListMaster Select N ' Huawei ', 1001,100,200 union ALL Sele CT n ' Huawei ', 1001,100,300 union ALL Select N ' Huawei ', 1001,100,301 union ALL Select N ' ZTE ', 1002, 200,1 Uni On all select N ' ZTE ', 1002, 200,2 select * from @InvoiceListMaster DELETE A from (select Rown = Row_nu
Mber () over (partition by CompanyName, InvoiceNumber, CustomerNumber ORDER by CompanyName, InvoiceNumber, CustomerNumber), CompanyName, InvoiceNumber, CustomerNumber from @InvoiceListMaster) a where exi STS (select 1 from (Select Rown = Row_number () over (partition by CompanyName, InvoiceNumber, Customernumbe R ORDER by CompanyName, InvoiceNumber, CustomerNumber), CompanyName, InvoiceNumber, Customernumb ER from @InvoiceListMaster) b where b.companyname = A.companyname and B.invoicenumber = A.invoicenumber and B.customernumber = A.
CustomerNumber and A.rown > B.rown) SELECT * FROM @InvoiceListMaster
The above example shows the SQL Server database to delete the data set duplication process, I hope this introduction can be harvested for you!