我想这样就行了
declare @t table (id int, name varchar(10), city varchar(10));
insert into @t values
(1, 'Joe', null)
, (1, 'Joe', 'DC')
, (2, 'Pete', 'NY')
, (2, null, 'NY')
, (3, null, 'TX')
, (5, 'Harry', null)
, (4, null, null);
select *
from ( select *
, case when name is null then 0 else 1 end
+ case when city is null then 0 else 1 end
as rowCnt
, row_number() over (partition by id order by case when name is null then 0 else 1 end
+ case when city is null then 0 else 1 end desc) as rn
from @t
) tt
where tt.rn = 1
order by tt.id
id name city rowCnt rn
----------- ---------- ---------- ----------- --------------------
1 Joe DC 2 1
2 Pete NY 2 1
3 NULL TX 1 1
4 NULL NULL 0 1
5 Harry NULL 1 1