将两个计数除以Oracle SQL

将两个计数除以Oracle SQL,sql,oracle,Sql,Oracle,我试图写一个我认为相当简单的查询,但事实证明比我想象的要难。我想将两个select语句的结果分开: Select count (*) from tableA where columnA = 'A' / Select count (*) from tableA where columnA in ('1','2') 我正在使用Oracle SQL Developer 感谢您的指导您可以将这两个查询作为单个主查询中的子查询: select (select count (*) from tabl

我试图写一个我认为相当简单的查询,但事实证明比我想象的要难。我想将两个select语句的结果分开:

Select count (*) from tableA where columnA = 'A'
/
Select count (*) from tableA where columnA in ('1','2')
我正在使用Oracle SQL Developer


感谢您的指导

您可以将这两个查询作为单个主查询中的子查询:

select 
  (select count (*) from tableA where columnA = 'A')
/
  (select count (*) from tableA where columnA in ('1', '2'))
from dual
或者,如果表名和列名实际上相同,则可以使用条件计数执行单个查询,例如:

select count (case when columnA = 'A' then columnA end)
       /
       count (case when columnA in ('1', '2') then columnA end)
from tableA
where columnA in ('A', '1', '2')
我已经修复了这两个问题中的单引号,不确定这是否只是发布问题的一个问题


如果第二次计数为零,您可能还需要添加逻辑来处理,因为这会导致运行时错误。

我建议使用条件聚合。但不算总数:


注意分母中缺少else。这将处理被零除的情况。

如果包含发生的情况(如果结果是错误的或您得到的错误),这将很有帮助。你真的用那些引号还是直接的单引号?
Select (sum(case when columnA = 'A' then 1 else 0 end) /
        sum(case when columnA in ('1', '2') then 1 end)
       )
from tableA;