统计多个维度的百分比

                                                     
SELECT
    CONCAT(
        ROUND(
            t1.total / t2.totalCount * 100,
            2
        ),
        '%'
    ) AS LocalPercent, t1.school_task_id, 0 as NonlocalPercent
FROM
    (
     select count(a.student_task_id) AS total ,school_task_id FROM tb_student_task a
     LEFT JOIN tb_student_result b on b.student_task_id=a.student_task_id
         WHERE a.task_id =8 and is_local_register=1 GROUP BY school_task_id
    ) t1,
    (
        SELECT
            count(*) AS totalCount
        FROM
            tb_student_task where is_local_register=1 and task_id=8
    ) t2 
     UNION All
        SELECT
  0 as LocalPercent , t1.school_task_id, CONCAT(
        ROUND(
            t1.total / t2.totalCount * 100,
            2
        ),
        '%'
    ) AS NonlocalPercent
FROM
    (
     select count(a.student_task_id) AS total ,school_task_id FROM tb_student_task a
     LEFT JOIN tb_student_result b on b.student_task_id=a.student_task_id
         WHERE a.task_id =8 and is_local_register=0 GROUP BY school_task_id
    ) t1,
    (
        SELECT
            count(*) AS totalCount
        FROM
            tb_student_task where is_local_register=0 and task_id=8
    ) t2 
原文地址:https://www.cnblogs.com/chongyao/p/13390084.html