下面是我正在使用的表格。
我想做的是获取每个 emp_id 的总行数作为 total_count,然后获取每个 emp_id 的总行数,其中 tos >0 作为 total_eligible,然后按 total_eligible/total_count 对它们进行排序。
最佳答案
您可以使用条件聚合来做到这一点:
select emp_id, count(*) as total,
sum(case when tos > 0 then 1 else 0 end) as total_eligible,
avg(case when tos > 0 then 1.0 else 0 end) as total_eligible_ratio
from table t
group by emp_id
order by total_eligible * 1.0 / total;
Here是一个 SQL fiddle 。
关于mysql - 根据行数与行数除以where条件的结果对所有记录进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814730/