I would like to do a count of active participants based on a field called program_code
grouped by the name of the manager. 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'
I can't put that in a case
statement with all those conditions. Is there a way I can do it? Would it need to be a sub-query in the select
clause? I have tried it with a sub-query in the select
clause, but it doesn't group it by 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.