提问者:小点点

MySQL计算百分比


我有一个包含4个项的MySQL数据库:ID(数值)、group_nameemployeessurves

在我的select中,我需要根据“surveys”中的数字来计算接受调查的“employees”的百分比。

这就是我现在的说法:

SELECT
  group_name,
  employees,
  surveys,
  COUNT( surveys ) AS test1, 
  ((COUNT( * ) / ( SELECT COUNT( * ) FROM a_test)) * 100 ) AS percentage
FROM
  a_test
GROUP BY
  employees

下面是表格的原样:

INSERT INTO a_test (id, group_name, employees, surveys) VALUES
(1, 'Awesome Group A', '100', '0'),
(2, 'Awesome Group B', '200', '190'),
(3, 'Awesome Group C', '300', '290');

我想根据调查中的数字计算参加调查的雇员的百分比。即,如上面的数据所示,Awesome GroupA将为0%,Awesome GroupB将为95%。


共1个答案

匿名用户

试试这个

   SELECT group_name, employees, surveys, COUNT( surveys ) AS test1, 
        concat(round(( surveys/employees * 100 ),2),'%') AS percentage
    FROM a_test
    GROUP BY employees

此处演示