简体   繁体   中英

Combine two queries in Oracle

I have 2 queries to retrieve faultCount and responseCount as follows and it works fine.

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;

I need to join to get the columns like this: COMP_IDENTIFIER,faultCount,responseCount . The following query does the job. But it takes a long time to execute (> 16 sec).

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;

I'm looking for a simple and faster query. Thanks in advance.

One possible reason this is taking longer is that you're reading all rows in CORDYS_NCB_LOG even where AUDIT_CONTEXT is not FAULT or RESPONSE , which are the only rows you're interested in.

You can add this to the WHERE clause of your existing query:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM