2014-10-10 57 views
1

我有以下幾點:查詢檢索所有相關的值都爲空組

| Group_ID | Value | 
|----------|-------| 
|  146 | A | 
|  146 | B | 
|  239 | NULL | 
|  239 | F | 
|  826 | NULL | 
|  826 | NULL | 


我需要檢索僅具有所有相關的值無效的ID。 在這個例子中,輸出將是​​。

我知道這似乎是一個簡單的查詢,但我很長時間以來一直在努力。

+2

你一飽眼福:P – HoneyBadger 2014-10-10 12:48:53

回答

5
select group_id 
from t 
group by group_id having sum(case when value is null then 1 end)=count(*) 
+0

+1儘管我更喜歡'不是null ... = 0' – FuzzyTree 2014-10-10 12:55:27

2

這應做到:

select t1.group_id 
from foo t1 
group by t1.group_id 
having count(*) = (select count(*) 
        from foo t2 
        where t2.group_id = t1.group_id 
        and t2.value is null); 

SQLFiddle例如:http://sqlfiddle.com/#!15/7d228/1

2
SELECT DISTINCT Group_ID 
FROM Your_Table a 
WHERE Value IS NULL 
    AND NOT EXISTS (
        SELECT NULL 
        FROM  Your_Table b 
        WHERE Value IS NOT NULL 
         AND b.Group_ID = a.Group_ID 
        ) 
0

試試這個:

select distinct Group_id 
from test 
where value is null 
    and Group_id not in (select Group_id from test where value is not null)