簡體   English   中英

在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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM