我想获得所有未经检查失败的汽车清单,但我不知道如何。我目前最好的是已经过去的汽车列表。
SELECT c.CAR_MAKE, c.CAR_MODEL, c.CAR_REGISTRATION, COUNT(*) as "Inspections Passed"
from inspection i
INNER JOIN car c
on i.CAR_ID = c.CAR_ID
WHERE i.INSPECTION_PASSED = '1'
GROUP BY c.CAR_MAKE,c.CAR_MODEL,c.CAR_REGISTRATION
HAVING count(*) >=1;
提前致谢!
答案 0 :(得分:2)
您想要的是所有组中的行inspection_passed = '1'
。所以,让我们算一下:
SELECT c.CAR_MAKE, c.CAR_MODEL, c.CAR_REGISTRATION,
COUNT(*) as "Inspections Passed"
FROM inspection i INNER JOIN
car c
on i.CAR_ID = c.CAR_ID
GROUP BY c.CAR_MAKE, c.CAR_MODEL, c.CAR_REGISTRATION
HAVING count(*) = SUM(case when i.INSPECTION_PASSED = '1' then 1 else 0 end);
如果INSPECTION_PASSED
仅使用值0
,1
和NULL
,您可以执行以下操作:
HAVING count(*) = SUM(INSPECTION_PASSED);
答案 1 :(得分:2)
您可以生成一组数据,其中包含所有检查失败并且使用不存在的汽车......对子查询进行协调。不知道为什么你需要计数或有。
SELECT c.CAR_MAKE, c.CAR_MODEL, c.CAR_REGISTRATION
FROM car c
WHERE NOT EXISTS (SELECT 1
FROM inspection I
WHERE I.Car_ID = C.car_ID
and inspection_passed <> '1')
这假定&lt;&gt; &#39; 1&#39;意味着失败。
或将inspection_passed <> '1'
更改为inspection_passed = '0'
)