sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo combinar dos consultas de conteo a su proporción?

Poner esta respuesta ya que ninguna ofrecida hasta ahora es correcta

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")

Si también necesitas los conteos individuales

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")

Nota:MySQL no tiene un problema de división por cero. Devuelve NULL cuando Rejected es 0.