,我需要選擇和組SQL組結果只有ID的所有他們ConditionId
值都1
具體標準
ID ConditionId
2 0
2 1
2 0
3 0
3 0
4 1
4 1
結果應該是:
ID ConditionId
4 1
我該怎麼做?
,我需要選擇和組SQL組結果只有ID的所有他們ConditionId
值都1
具體標準
ID ConditionId
2 0
2 1
2 0
3 0
3 0
4 1
4 1
結果應該是:
ID ConditionId
4 1
我該怎麼做?
這可以通過檢查每個id的行數是否等於表中每個id的conditionid = 1的行數來完成。
select id
from tablename
group by id
having count(*) = count(case when conditionid = 1 then 1 end)
SELECT DISTINCT ID, ConditionId
FROM Table
WHERE ConditionId=1
AND ID NOT IN (SELECT ID FROM Table WHERE NOT ConditionId = 1)
我會傾向於做:
select id
from t
group by id
having max(ConditionId) = min(ConditionId) and max(ConditionId) = 1;
假設你只有在數據0和1,這應該工作:
select id
from t
group by id
having min(ConditionId) = 1;
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;
請提供一些信息,以幫助使此答案更有意義。 – Nicarus