我有一张像这样的小桌子:
****************************
| Name | old_v | new_v *
****************************
|ITEM1 | new | passed *
|ITEM1 | failed | passed *
|ITEM2 | new | failed *
|ITEM1 | passed | failed *
|ITEM3 | new | blocked *
|ITEM2 | failed | failed *
|ITEM4 | new | new *
我尝试构建一个sql查询,向我显示所有项目,从不状态已通过。 根据上面的例子,输出应该是:
ITEM2
ITEM3
但是,这个查询:
SELECT Name FROM table WHERE old_v NOT IN 'passed' AND new_v NOT IN 'passed'
不能给我正确的结果。你有什么线索如何解决这个问题吗?
答案 0 :(得分:3)
如果没有通过具有相同名称的行,请使用NOT EXISTS
返回一行:
select distinct t1.name
from tablename t1
where not exists (select 1 from tablename t2
where t2.name = t1.name
and 'passed' in (t2.old_v, t2.new_v))
答案 1 :(得分:3)
如果您需要的所有数据都在同一个表格中而您只需要名称,则可以汇总数据:按名称分组并使用HAVING
删除不需要的记录:
select name
from mytable
group by name
having count(case when old_v = 'passed' then 1 end) = 0
and count(case when new_v = 'passed' then 1 end) = 0;
答案 2 :(得分:0)
试试这个:
SELECT
item
FROM
[TABLE] t LEFT OUTER JOIN
(SELECT
item
FROM
[TABLE]
WHERE
old_v = 'passed' OR
new_v = 'passed') p ON
t.item = p.item
WHERE
p.item IS NULL
GROUP BY
item