我有以下幾點:查詢檢索所有相關的值都爲空組
| Group_ID | Value |
|----------|-------|
| 146 | A |
| 146 | B |
| 239 | NULL |
| 239 | F |
| 826 | NULL |
| 826 | NULL |
我需要檢索僅具有所有相關的值無效的ID。 在這個例子中,輸出將是。
我知道這似乎是一個簡單的查詢,但我很長時間以來一直在努力。
我有以下幾點:查詢檢索所有相關的值都爲空組
| Group_ID | Value |
|----------|-------|
| 146 | A |
| 146 | B |
| 239 | NULL |
| 239 | F |
| 826 | NULL |
| 826 | NULL |
我需要檢索僅具有所有相關的值無效的ID。 在這個例子中,輸出將是。
我知道這似乎是一個簡單的查詢,但我很長時間以來一直在努力。
select group_id
from t
group by group_id having sum(case when value is null then 1 end)=count(*)
+1儘管我更喜歡'不是null ... = 0' – FuzzyTree 2014-10-10 12:55:27
這應做到:
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
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
)
試試這個:
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)
你一飽眼福:P – HoneyBadger 2014-10-10 12:48:53