SQL Server数据库删除数据集中重复数据实例讲解

编程学习 2021-07-05 13:43www.dzhlxh.cn编程入门
本文通过一个例子介绍了SQL Server数据库中删除数据集中的重复数据的操作过程,需要的朋友可以参考下

SQL Server数据库操作中,有时对于表中的结果集,满足一定规则我们则认为是重复数据,而这些重复数据需要删除。如何删除呢?本文我们通过一个例子来加以说明。

例子如下:

如下只要companyName,invoiceNumber,customerNumber三者都相同,我们则认为是重复数据,狼蚁网站SEO优化的例子演示了如何删除。

declare @InvoiceListMaster table ( ID int identity primary key ,  
 
companyName Nchar(20),  
 
invoiceNumber int,  
 
CustomerNumber int,  
 
rmaNumber int )  
 
insert  @InvoiceListMaster  
 
select N'华为', 1001,100,200  
 
union all  
 
select N'华为', 1001,100,300  
 
union all  
 
select N'华为', 1001,100,301  
 
union all  
 
select N'中兴', 1002, 200,1     
 
union all  
 
select N'中兴', 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 = a.companyName  
 
and b.invoiceNumber = a.invoiceNumber  
 
and b.CustomerNumber = a.CustomerNumber  
 
and a.rown > b.rown  
 
)  
 
select * from @InvoiceListMaster 

以上的例子就演示了SQL Server数据库删除数据集中重复数据的过程,希望本次的介绍能够对您有所收获!

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板