Oracle统计某一字段在不同值下的总数

时间:2021-05-15 15:11:36

开发遇到一个sql查询问题,需求就系统计某表某一字段在不同值下的总数,若用count会把所有行数统计起来而不是分值统计,所以采用sum进行统计。

SELECT t1.pid,
t1.name AS pxname,
t1.cost,
t1.type,
t1.sponsor,
t1.organizer,
count(t2.pid) as zrs,
sum(case when t2.is_paid='1' then 1 else 0 end) as yjfzrs,
sum(case when t2.is_paid='0' then 1 else 0 end) as wjfzrs
FROM T_ZGDX_WJ_TRAINING_PLAN t1 left join t_zgdx_wj_stu_enroll t2 on t1.pid = t2.t_id
WHERE t1.modify_flag<>'D'
AND t1.type <>'公务员全员培训'
group by (
t1.pid,
t1.name,
t1.cost,
t1.type,
t1.sponsor,
t1.organizer
);