Home > Database > Mysql Tutorial > body text

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

DDD
Release: 2024-10-25 12:41:30
Original
507 people have browsed it

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
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!