添加列作为已选择的另外2个操作的结果

时间:2015-07-22 19:28:42

标签: mysql sql

我有这个SQL查询:

SELECT   `manager` AS manager,
(SELECT COUNT(*) FROM `las-olas` where total_general>=0 and total_general<80 and manager=ol.manager and ola=ol.ola) as critical,
COUNT(1) as total
FROM `las-olas` ol
where zona="East" and ola=6
GROUP BY manager
order by critical DESC, manager

给我带来了这个结果:

manager    critical    total
Critobal   9           25
Miguel     9           25
Cesar      8           37
Jesus      3           29

我想在结果表中添加另一个名为“百分比”的列,该列是每行 critical/total*100 的结果:

manager    critical    total    percent
Critobal   9           25       36
Miguel     9           25       36
Cesar      8           37       22
Jesus      3           29       10

1 个答案:

答案 0 :(得分:0)

您可以使用另一个执行计算的选择

来包围该查询
select manager, critical, total, critical * 100/total from
            (SELECT   `manager` AS manager,
    (SELECT COUNT(*) FROM `las-olas` where total_general>=0 and
        total_general<80 and manager=ol.manager and ola=ol.ola) as critical,
     COUNT(1) as total
     FROM `las-olas` ol
     where zona="East" and ola=6
     GROUP BY manager
     order by critical DESC, manager) TotalCounts