我有4个项目MySQL数据库:id(数值)group_name,employees和surveys。
id
group_name
employees
surveys
在我的报告中,SELECT我需要通过“调查”中的数字来计算已参加调查的“雇员”的百分比。
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');
我想employees按surveys参加调查的人数来计算谁所占的百分比。即,如以上数据所示,分别Awesome Group A为0%和Awesome Group B95%。
Awesome Group A
Awesome Group B
尝试这个
SELECT group_name, employees, surveys, COUNT( surveys ) AS test1, concat(round(( surveys/employees * 100 ),2),'%') AS percentage FROM a_test GROUP BY employees
在这里演示