Calculating Percentage in MySQL
When working with a MySQL database, it is often necessary to calculate the percentage of a value. In particular, a common requirement is to find the percentage of employees who, based on the number of surveys, have participated in a survey.
Problem:
Consider a MySQL table named a_test with the following columns: id, group_name, employees, and surveys. The task is to calculate the percentage of employees within each group who have taken the survey.
Solution:
The following revised SQL statement incorporates a modification that effectively calculates the percentage:
<code class="sql">SELECT group_name, employees, surveys, COUNT(surveys) AS test1, concat(round((surveys/employees * 100),2), '%') AS percentage FROM a_test GROUP BY employees;</code>
The concat function is used to combine the calculated percentage with the percent sign (%) and round the result to two decimal places for better readability.
Explanation:
With this modified query, you can accurately determine the percentage of employees within each group who have participated in the survey, providing valuable insights for your data analysis.
The above is the detailed content of How to Calculate the Percentage of Employees Who Participated in a Survey in MySQL?. For more information, please follow other related articles on the PHP Chinese website!