计算已完成任务mysql中的百分比总数

时间:2019-02-05 03:41:49

标签: mysql percentage

我需要分组并在MySQL查询中以真实方式完成百分比

id type completed
1   4    true
2   4    false
3   3    false
4   5    true
5   5    true

结果

type   completed
4        %50
3        %0
5        %100

1 个答案:

答案 0 :(得分:1)

尝试使用条件聚合:

SELECT
    type,
    100.0 * AVG(completed = 'true') AS completed
FROM yourTable
GROUP BY
    type;

Demo