2016-07-25 89 views
0

,我需要選擇和組SQL組結果只有ID的所有他們ConditionId值都1具體標準

ID  ConditionId 
    2  0 
    2  1 
    2  0 
    3  0 
    3  0 
    4  1 
    4  1 

結果應該是:

ID  ConditionId    
4  1 

我該怎麼做?

回答

1

這可以通過檢查每個id的行數是否等於表中每個id的conditionid = 1的行數來完成。

select id 
from tablename 
group by id 
having count(*) = count(case when conditionid = 1 then 1 end) 
0
SELECT DISTINCT ID, ConditionId 
FROM Table 
WHERE ConditionId=1 
    AND ID NOT IN (SELECT ID FROM Table WHERE NOT ConditionId = 1) 
+0

請提供一些信息,以幫助使此答案更有意義。 – Nicarus

0

我會傾向於做:

select id 
from t 
group by id 
having max(ConditionId) = min(ConditionId) and max(ConditionId) = 1; 
1

假設你只有在數據0和1,這應該工作:

select id 
from t 
group by id 
having min(ConditionId) = 1; 
0
select id 
    from t 
    group by id 
    having sum(abs(1-ConditionId)) = 0; 

select distinct id from t 
minus 
select distinct id from t where ConditionId <> 1;