我正在尝试编写一个我认为是相当简单的查询,但事实证明比我想象的要难。我想将两个select语句的结果分开:
Select count (*) from tableA where columnA = 'A'
/
Select count (*) from tableA where columnA in ('1','2')我正在使用Oracle SQL Developer
感谢您的指导
发布于 2019-02-17 05:12:55
我建议使用条件聚合。。。但是使用sum()而不是count()
Select (sum(case when columnA = 'A' then 1 else 0 end) /
sum(case when columnA in ('1', '2') then 1 end)
)
from tableA;请注意,分母中缺少else。这将处理被零除的情况。
发布于 2019-02-17 04:09:18
您可以将两者视为单个主查询中的子查询:
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')我已经修复了两者中的单引号,不确定这些是否只是发布您的问题的问题。
您可能还需要添加逻辑来处理第二个计数为零,如果这可能发生的话-因为这将导致运行时错误。
https://stackoverflow.com/questions/54726896
复制相似问题