MySQL MySQL计算百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15746749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:08:19  来源:igfitidea点击:

MySQL Calculate Percentage

mysqlpercentage

提问by user2232709

I have a MySQL database with 4 items: id(numerical), group_name, employees, and surveys.

我有一个包含 4 个项目的 MySQL 数据库:(id数字)group_nameemployees、 和surveys

In my SELECTI need to calculate the percentage of 'employees' who, by the number in 'surveys', have taken the survey.

在我SELECT我需要计算谁,在“调查”的数量,所采取的调查,“雇员”的百分比。

This is the statement I have now:

这是我现在的声明:

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

Here is the table as it stands:

这是它的表格:

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');

I would love to calculate the percentage of employeeswho by the number in surveyshave taken the survey. i.e. as shown in the data above, the Awesome Group Awould be 0% and Awesome Group Bwould be 95%.

我很想employees通过surveys参加调查的人数来计算谁的百分比。即如上面的数据所示,Awesome Group A将是 0% 和Awesome Group B95%。

回答by echo_Me

try this

尝试这个

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

DEMO HERE

演示在这里