我有一个具有以下结构的“建议”表:

id(suggest_id) | author_id | accepted(true/false)


我想以最大的接受率订购,例如:

Jack had 10 suggests and he accepted 5 (50% acceptance rate)
John had 20 suggests and he accepted 5 (25% acceptance rate)
Steve had 10 suggests and he accepted 8 (80% acceptance rate)


这将返回:史蒂夫,杰克和约翰。

我认为可能必须使用两个SQL查询,一个用于建议数量,第二个用于accepted=true

也许可以通过一个查询来完成?

我正在使用rails,因此也可以通过rails完成。

最佳答案

根据您表示接受(真/假)的方式,例如...

select author_id,
    sum(case when accepted ='true' then 1 else 0 end),
    100.0*sum(case when accepted ='true' then 1 else 0  end)/count(*)
from yourtable
group by author_id
order by 100.0*sum(case when accepted ='true' then 1 else 0 end)/count(*) desc

关于mysql - sql语句的接受率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12797251/

10-11 05:26