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:
The above is the detailed content of How to Calculate the Percentage of Employees Who Participated in Surveys in MySQL?. For more information, please follow other related articles on the PHP Chinese website!