如何将两个计数查询结合到它们的比率? [英] How to combine two count queries to their ratio?
本文介绍了如何将两个计数查询结合到它们的比率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个查询:
select count(*) from my_table where status="accepted"
和
select count(*) from my_table where status="rejected"
拒绝,所以我想知道是否可以组合这两个查询,所以我不必执行两个查询
I was to find the ratio of accepted/reject so I Was wondering if it's possible to combine the two queries so I don't have to execute two queries
推荐答案
select count(case when status = "accepted" then 1 end) /
count(case when status = "rejected" then 1 end) as Ratio
from my_table
where status in ("accepted","rejected")
如果您还需要个别计数
select count(case when status = "accepted" then 1 end) Accepted,
count(case when status = "rejected" then 1 end) Rejected,
count(case when status = "accepted" then 1 end) /
count(case when status = "rejected" then 1 end) as Ratio
from my_table
where status in ("accepted","rejected")
注意:MySQL没有除零问题。当Rejected为0时,它返回NULL。
这篇关于如何将两个计数查询结合到它们的比率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文