MySQL计算百分比

发布于 2021-02-02 16:26:02

我有4个项目MySQL数据库:id(数值)group_nameemployeessurveys

在我的报告中,SELECT我需要通过“调查”中的数字来计算已参加调查的“雇员”的百分比。

这是我现在的声明:

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

我想employeessurveys参加调查的人数来计算谁所占的百分比。即,如以上数据所示,分别Awesome Group A为0%和Awesome Group B95%。

关注者
0
被浏览
136
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    尝试这个

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

    在这里演示



推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看