具有多个条件的案例陈述

时间:2014-02-14 19:43:42

标签: sql oracle

我想根据经理名称分组的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

1 个答案:

答案 0 :(得分: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