在 Oracle 中合并两个查询

Combine two queries in Oracle

我有 2 个查询来检索 faultCountresponseCount,如下所示,它工作正常。

select count(*) as faultCount,
       COMP_IDENTIFIER 
from CORDYS_NCB_LOG 
where AUDIT_CONTEXT='FAULT' 
group by COMP_IDENTIFIER  
order by responseCount;

select count(*) as responseCount,
       COMP_IDENTIFIER 
from CORDYS_NCB_LOG 
where AUDIT_CONTEXT='RESPONSE' 
group by COMP_IDENTIFIER  
order by responseCount;

我需要加入才能获得这样的列:COMP_IDENTIFIER,faultCount,responseCount。以下查询完成这项工作。但是执行需要很长时间(> 16秒)。

select count(case AUDIT_CONTEXT when 'FAULT'    then 1 end) as faultCount,
       count(case AUDIT_CONTEXT when 'RESPONSE' then 1 end) as responseCount,
       COMP_IDENTIFIER 
from CORDYS_NCB_LOG 
group by COMP_IDENTIFIER  
order by responseCount;

我正在寻找一个简单而快速的查询。提前致谢。

这需要更长时间的一个可能原因是您正在读取 CORDYS_NCB_LOG 中的所有行,即使 AUDIT_CONTEXT 不是 FAULTRESPONSE,它们是只有您感兴趣的行。

您可以将其添加到现有查询的 WHERE 子句中:

select count(case AUDIT_CONTEXT when 'FAULT'    then 1 end) as faultCount,
       count(case AUDIT_CONTEXT when 'RESPONSE' then 1 end) as responseCount,
       COMP_IDENTIFIER 
from CORDYS_NCB_LOG
where AUDIT_CONTEXT in ('FAULT', 'RESPONSE')
group by COMP_IDENTIFIER  
order by responseCount;