我试图改进以前编写的查询。这是该查询-
select tq.feature as Feature, tq.Total, pq.Passed
from (
select feature, count(distinct id) as Total
from X.results
where ver = '4.2'
group by feature
) as tq
LEFT JOIN (
select feature, count(distinct id) as Passed
from X.results
where ver = '4.2' and result = 'pass'
group by feature
) as pq USING (feature);
这是我写的查询。但结果似乎不同。我在这里遗漏了一些东西吗?
select feature,count(distinct id) as totalcases,
sum(case when result = 'PASS' then 1 else 0 end) as passed
from X.results
where ver='4.2'
group by feature
order by feature;
我是一个真正的 sql 菜鸟,所以如果有什么愚蠢的事情请原谅我..
最佳答案
一种方法是根据结果和功能进行分组,然后仅对功能再次进行聚合。像这样的东西。
SELECT
feature,
SUM(id_count) totalcases,
SUM(CASE WHEN result = 'PASS' THEN id_count ELSE 0 END) passed
FROM
(
SELECT
feature,
CASE WHEN result = 'PASS' THEN 'PASS' ELSE 'OTHER' END result,
COUNT(distinct id) id_count
FROM x.results
WHERE ver='4.2'
GROUP BY feature,CASE WHEN result = 'PASS' THEN 'PASS' ELSE 'OTHER' END
) x
GROUP BY feature
ORDER BY feature
关于mysql - 这些查询有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691752/