Question

I have an encountered problem in my query in Oracle SQL. I don't know how to get the sum of this query:

select call_type, channel
,count (case when status="no answer" then 1 end else 0) as cnt_no_answer
,count (case when status="answered" then 1 end else 0) as cnt_answer
from app_account.cc_call;

Please Help me. Thanks!

Was it helpful?

Solution

To get the answered and not answered records sum instead of count. To get the number of all records that are either answered or not answered use count(status). To get the count of all records, i.e. also records with status null use count(*). Strings need single quotes, not double quotes. The case statement needs END.

EDITED (there were too many END used):

select call_type, channel
  , sum(case when status='no answer' then 1 else 0 end) as cnt_no_answer
  , sum(case when status='answered' then 1 else 0 end) as cnt_answer
  , count(status) as cnt_all_stated
  , count(*) as cnt_all_records
from app_account.cc_call
group by call_type, channel;

OTHER TIPS

try this again, i edit it:

SELECT call_type, channel,
       sum (CASE WHEN status='no answer' THEN 1 ELSE 0 END) AS cnt_no_answer,
       sum (CASE WHEN status='answered' THEN 1 ELSE 0 END) AS cnt_answer
FROM app_account.cc_call
GROUP BY call_type, channel;

Check this out:

SELECT   CALL_TYPE,
       CHANNEL,
       COUNT (CASE WHEN UPPER(STATUS) = UPPER('no answer') THEN 1 ELSE NULL END)
           AS CNT_NO_ANSWER,
       COUNT (CASE WHEN UPPER(STATUS) = UPPER('answered') THEN 1 ELSE NULL END)
           AS CNT_ANSWER
FROM   APP_ACCOUNT.CC_CALL
GROUP BY   CALL_TYPE, CHANNEL;

select call_type, channel

, sum(case when status='no answer' then 1 else 0 end) as cnt_no_answer

, sum(case when status='answered' then 1 else 0 end) as cnt_answer

, count(status) as cnt_all_stated

, count(*) as cnt_all_records

from app_account.cc_call

group by call_type, channel;

why usind end twice

SELECT Call_type, Channel
,COUNT(CASE WHEN status="no answer" then 1 else 0 end ) as Cnt_no_answer
,COUNT(CASE WHEN status="answered" then 1 else 0 end ) as Cnt_answer
from App_account.cc_call 
GROUP BY call_type,channel;

I think there is some error in table deceleration. Pleas give table structure. And you want SUM and YOU are COUNTING there I dnt think it is error but lack of overview. Please give some more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top