Description of duplicate data instances in a dataset deleted from the SQL Server database, and description of server instances

Source: Internet
Author: User

Description of duplicate data instances in a dataset deleted from the SQL Server database, and description of server instances

In SQL Server database operations, when the result set in the table meets certain rules, we think it is duplicated data, and the duplicated data needs to be deleted. How to delete it? This article uses an example to describe.

Example:

As long as companyName, invoiceNumber, and mermernumber are all the same, we think they are duplicate data. The following example shows how to delete them.

Declare @ InvoiceListMaster table (ID int identity primary key, companyName Nchar (20), invoiceNumber int, CustomerNumber int, rmaNumber int) insert @ InvoiceListMaster select N 'huawei ', 1001,100,200 union all select N 'huawei ', 1001,100,300 union all select N 'huawei', 1001,100,301 union all select N 'zte ', 1002,200, 1 union all select N 'zte ', 1002,200, 2 select * from @ InvoiceListMaster delete a from (select rown = ROW_NUMBER () over (partition by companyname, invoicenumber, customerNumber order by companyname, invoicenumber, customerNumber), companyname, invoicenumber, customerNumber from @ InvoiceListMaster) a where exists (select 1 from (select rown = ROW_NUMBER () over (partition by companyname, invoicenumber, customerNumber order by companyname, invoicenumber, customerNumber), companyname, invoicenumber, customerNumber from @ InvoiceListMaster) B where B. companyName =. companyName and B. invoiceNumber =. invoiceNumber and B. customerNumber =. customerNumber and. rown> B. rown) select * from @ InvoiceListMaster

The preceding example demonstrates the process of deleting duplicate data in a dataset in the SQL Server database. We hope this introduction will be helpful to you!

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.