日期:2014-05-18 浏览次数:20608 次
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test](
[ID] int,
[name] varchar(1),
[var1] varchar(2),
[var2] varchar(2),
[var3] varchar(2)
)
insert [test]
select 1,'A',null,'A2','A3' union all
select 2,'B','B2',null,null union all
select 3,'B','B1','B2',null union all
select 4,'C',null,'C2','C3' union all
select 5,'C','C1',null,null union all
select 6,'C','C1','C2','C3'
with t
as(
select *,
case when [var1] is null then 1 else 0 end+
case when [var2] is null then 1 else 0 end+
case when [var3] is null then 1 else 0 end as totals
from test
)
select [ID],[name],[var1],[var2],[var3] from t a
where totals=(select MIN(totals) from t b where a.name=b.name)
/*
ID name var1 var2 var3
1 A NULL A2 A3
3 B B1 B2 NULL
6 C C1 C2 C3
*/