"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > 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?

Published on 2024-11-16
Browse:908

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.
Latest tutorial More>

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