您需要一些方法来区分要删除的行和要保留的行。我someOtherColumn
在下面使用来实现这一点:
create table #DraftInvoiceRecords (
employee int not null,
amount int not null,
units int not null,
someOtherColumn int not null
)
create table #ReversedRecords (
employee int not null,
amount int not null,
units int not null
)
insert into #DraftInvoiceRecords (employee,amount,units,someOtherColumn)
select 1,1,1,1 union all
select 1,1,1,2
insert into #ReversedRecords (employee,amount,units)
select 1,1,1
delete from dir
from
#DraftInvoiceRecords dir
inner join
#ReversedRecords rr
on
dir.employee = rr.employee and
dir.amount = rr.amount and
dir.units = rr.units
left join
#DraftInvoiceRecords dir_anti
on
dir.employee = dir_anti.employee and
dir.amount = dir_anti.amount and
dir.units = dir_anti.units and
dir.someOtherColumn > dir_anti.someOtherColumn --It's this condition here that allows us to distinguish the rows
where
dir_anti.employee is null
select * from #DraftInvoiceRecords
drop table #DraftInvoiceRecords
drop table #ReversedRecords