Combine two queries in Oracle

87 views Asked by At

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.

1

There are 1 answers

0
TobyLL On BEST ANSWER

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;