如何检索失败的迭代SQL

时间:2014-09-24 04:44:43

标签: sql

我有数据如下

ID Value
1  Failed
1  Failed
1  Failed
1  Passed
1  Failed
2  Failed
2  Failed
2  Failed
2  Failed
2  Failed

我想使用SQL仅检索Failed Iterations的ID。我该怎么做?

2 个答案:

答案 0 :(得分:2)

如果我正确理解了您的问题,您只想返回ids 所有 values等于failed的{​​{1}}。

如果是,您可以使用group by

select id
from yourtable
group by id
having count(*) = sum(case when value = 'Failed' then 1 else 0 end)

答案 1 :(得分:0)

这会对你有帮助,

select distinct a1.ID from B as a1
left join B as a2 on a1.id = a2.id  
                and a2.value <> 'failed'
where a1.value = 'failed'
    and a2.ID is null

虽然这会提供正确的结果,但我已经使用了join,但@sgeddes的回答看起来很棒!