在Oracle中合并两个查询

时间:2015-06-08 15:54:11

标签: sql oracle union

我有两个查询要检索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;

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

1 个答案:

答案 0 :(得分:3)

这需要更长时间的一个可能原因是,即使CORDYS_NCB_LOG不是AUDIT_CONTEXTFAULT,您也会在RESPONSE中阅读所有行。只有您感兴趣的行。

您可以将其添加到现有查询的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;