sql Server数据库操作中,有时对于表中的结果集,满足一定规则我们则认为是重复数据,而这些重复数据需要删除。如何删除呢?本文我们通过一个例子来加以说明。
例子如下:
如下只要companyName,invoiceNumber,customerNumber三者都相同,我们则认为是重复数据,下面的例子演示了如何删除。
select N'华为',1001,100,200
union all
select N'华为',300
union all
select N'华为',301
union all
select N'中兴',1002,200,1
union all
select N'中兴',2
select * from @InvoiceListMaster
DELETE A
from (
select rown = ROW_NUMBER( )over( partition by companyname,invoicenumber,customerNumber
order by companyname,customerNumber ),companyname,customerNumber
from @InvoiceListMaster )a
where exists ( select 1
from ( select rown = ROW_NUMBER( )over( partition by companyname,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