2017-08-11 59 views
0

如何: 选择组内所有行均为NULL的行。下面的组由id定义。 I 希望选择所有值为NULL的行。选择组内所有值均为NULL的行

Id Var1 
1 NULL 
1 NULL  
1 NULL 
2 10 
2 20 
2 30 
3 NULL 
3 30 

我曾尝试:

select id 
from table 
where all var1 is null 
group by id 

期望的结果:

id var1 
1 NULL 
1 NULL  
1 NULL 

回答

1

使用having而不是where

select id 
from table 
group by id 
having max(var1) is null; 

类似的方法使用:

having count(var1) = 0 
1

你可以用这个查询尝试:

select id,Var1 
from table1 as a 
where not exists (select id 
        from table1 as b 
        where a.id=b.id and b.Var1 is not null) 

子查询得到具有值不为空的ID之后聚集过滤,所以你不要让他们在主要查询