数据采样设置:
create table #data (CUSTACCOUNT varchar(50), DIVISION varchar(50), EXTPERSON varchar(50), SALESMAN varchar(50))
insert into #data values
('C0001729', 'ECD', '5637263283', 'Ian'),
('C0001729', 'Fuel', '5637369057', 'Peter'),
('C0001729', 'Fuel', NULL, 'House'),
('C0001729', 'ECD', NULL, 'House'),
('C0001729', 'BSC', '5637263239', 'Andrew'),
('C0001729', 'SomeOther', NULL, 'Name')
删除查询:
;with aData
as (
select
rn = row_number() over (partition by DIVISION order by EXTPERSON desc),
cntExt = count(EXTPERSON) over (partition by DIVISION)
from #data
)
delete from aData
where rn > cntExt and cntExt > 0
检查表数据:
select * from #data
输出:
CUSTACCOUNT DIVISION EXTPERSON SALESMAN
------------ ----------- ----------- ----------
C0001729 ECD 5637263283 Ian
C0001729 Fuel 5637369057 Peter
C0001729 BSC 5637263239 Andrew
C0001729 SomeOther NULL Name
请注意,您只需要选择的代码,然后单击编辑上面的代码按钮。你不需要使用html来格式化它。 [阅读](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) –