简体   繁体   中英

calculate the percentage total in completed tasks mysql

I need to group and get the percentage completed in true in a MySQL query

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

Result

type   completed
4        %50
3        %0
5        %100

Try using conditional aggregation:

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

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM