Home >Database >Mysql Tutorial >How to Calculate the Percentage of Employees Who Participated in Surveys in MySQL?

How to Calculate the Percentage of Employees Who Participated in Surveys in MySQL?

DDD
DDDOriginal
2024-10-25 12:41:30613browse

How to Calculate the Percentage of Employees Who Participated in Surveys in MySQL?

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:

  • Correct Calculation: The percentage is now calculated as ( surveys / employees * 100 ), providing the accurate proportion of surveyed employees.
  • Percentage Formatting: The result is formatted as a percentage string using concat and round functions to display a rounded percentage with two decimal places.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn