Calculating Percentage in MySQL
Within a MySQL database containing employee and survey data, a user sought to calculate the percentage of employees who participated in surveys based on the number of recorded surveys.
The original query attempted to derive the percentage using the following statement:
SELECT group_name, employees, surveys, COUNT( surveys ) AS test1, ((COUNT( * ) / ( SELECT COUNT( * ) FROM a_test)) * 100 ) AS percentage FROM a_test GROUP BY employees
However, this approach yielded incorrect results. To rectify the issue, a revised query was proposed:
SELECT group_name, employees, surveys, COUNT( surveys ) AS test1, concat(round(( surveys/employees * 100 ),2),'%') AS percentage FROM a_test GROUP BY employees
This modified query incorporates the following adjustments:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3