[英]Case statement with multiple conditions
I would like to do a count of active participants based on a field called program_code
grouped by the name of the manager.我想根据经理姓名分组的名为
program_code
的字段计算活跃参与者的数量。 But I also want a count of something else in the same query where three conditions have to be met.但我还想在同一个查询中计算其他必须满足三个条件的东西。
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
But I want a third count of all participants who have a possible_ind='Y' AND date_attended is not null AND status_cd='P'
但我希望对所有具有
possible_ind='Y' AND date_attended is not null AND status_cd='P'
参与者进行第三次计数
I can't put that in a case
statement with all those conditions.我不能把它放在具有所有这些条件的
case
语句中。 Is there a way I can do it?有什么办法可以做到吗? Would it need to be a sub-query in the
select
clause?是否需要成为
select
子句中的子查询? I have tried it with a sub-query in the select
clause, but it doesn't group it by manager_name
.我已经在
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
You can omit the expression in your case statement and make your "when" statements more detailed.您可以省略 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
Refer to Oracle's example for more information.有关详细信息,请参阅Oracle 的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.