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