我有一个看起来像这样的简单表格:
id,field
40,Agree
41,Agree
42,Disagree
43,Strongly agree
45,Strongly agree
46,Agree
47,Neutral
48,Agree
我想在字段为 Agree 或 Strongly agree 时输出完美的响应。在这种情况下,它将是 6/8 或 0.75。
我不太确定如何处理它,但我的猜测是我需要使用子查询来选择一个只包含我感兴趣的两个响应的表,然后将其除以原始表的行数.
感谢任何帮助。
谢谢!
最佳答案
一种简单的方法是使用条件聚合:
select avg(case when field in ('Agree', 'Strongly Agree') then 1.0 else 0
end) as ratio
from t;
关于mysql - SQL - 计算项目的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455954/