我需要选择并将其所有ConditionId
值均为1
ID ConditionId
2 0
2 1
2 0
3 0
3 0
4 1
4 1
结果应为:
ID ConditionId
4 1
我该怎么做?
答案 0 :(得分:1)
这可以通过检查每个id的行数是否等于表中每个id的conditionid = 1的行数来完成。
select id
from tablename
group by id
having count(*) = count(case when conditionid = 1 then 1 end)
答案 1 :(得分:1)
假设数据中只有0和1,这应该有效:
select id
from t
group by id
having min(ConditionId) = 1;
答案 2 :(得分:0)
SELECT DISTINCT ID, ConditionId
FROM Table
WHERE ConditionId=1
AND ID NOT IN (SELECT ID FROM Table WHERE NOT ConditionId = 1)
答案 3 :(得分:0)
我倾向于这样做:
select id
from t
group by id
having max(ConditionId) = min(ConditionId) and max(ConditionId) = 1;
答案 4 :(得分: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;