2014-02-14 70 views
0

我想根據經理名稱分組的program_code字段對活躍參與者進行計數。但是我也希望在同一個查詢中需要滿足三個條件的其他內容。具有多個條件的案例陳述

select manager_name, 
     sum (case program_code when 'F' then 1 else 0 end) as F, 
     sum (case program_code 
       when 'FS' then 1 
       else 0 
      end) 
    from table1 
where status='AC' 
group by manager_name 

但我想誰擁有possible_ind='Y' AND date_attended is not null AND status_cd='P'

我不能把在一個case聲明與所有這些條件的所有參與者的第三計。有沒有辦法我可以做到這一點?它是否需要成爲select子句中的子查詢?我用select子句中的子查詢對它進行了嘗試,但它沒有按manager_name對它進行分組。

select manager_name, 
     sum (program_code when 'F' then 1 else 0 end) as F, 
     sum (case program_code 
       when 'FS' then 1 
       else 0 end), 
      (select count(*) 
       from table1 
      where possible_ind='Y' 
       and date_attended is not null 
       and status_cd='P') as NEW 
    from table1 
where status='AC' 
group by manager_name 
+0

'status'和'status_cd'是兩個不同的列嗎? – peterm

回答

0

您可以省略case語句中的表達式,並使「when」語句更加詳細。

select manager_name, 
sum (program_code when 'F' then 1 else 0 end) as F, 
sum (case program_code 
    when 'FS' then 1 
    else 0 end), 
sum (case 
    when possible_ind='Y' 
     and date_attended is not null 
     and status_cd='P' then 1 else 0 end) as NEW 
from table1 
where status='AC' 
group by manager_name 

有關更多信息,請參閱Oracle's example

+0

這不起作用,因爲在where子句中,我的狀態='AC',在第三種情況下,我需要的狀態是P. – user1466935

+0

我接受了您的建議,爲案例表達式添加更多細節並刪除了條件在where子句中。作爲F,總和(case program_code ='FS'和status_cd ='AC'then 1 else 0 end)作爲FS,總和(case program_code ='F'和status_cd ='AC'then 1 else 0 end) (program_code ='F'和status_cd ='AC'時的情況),sum(case possible_ind ='Y'和date_attended不爲空且status_cd ='PN'then 1 else 0 end)然後1 else 0 end)> 1 – user1466935

+0

啊,我不知道他們是同一列。真高興你做到了。 – Sanders