简体   繁体   中英

How do i calculate how many employees fall into each category based on their scores SQL

I have a table called respondent,within that table there is a column called 'score' i need to be able to divide the respondents by percentage based on their user score.

More specifically, what % of respondents fall below 22.5 What percent of respondents fall between 25.0 and 47.5 what percent of respondents fall between 50.0 and 75.0 what percent of respondents fall above 75.0

How would i express this in a QUERY, I need it group by respondent. Please i cant figure it out.

You can use conditional aggregation. The syntax varies across databases, but a rather portable way to express this is:

select
    avg(case when score < 22.5 then 1.0 else 0 end) ratio1,
    avg(case when score >= 25 and score < 47.5 then 1.0 else 0 end) ratio2,
    ...
from mytable

This gives the ratio as a decimal value between 0 and 1 , which you can multiply by 100 if you want a percentage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM