Oracle統計某一欄位在不同值下的總數
阿新 • • 發佈:2019-02-08
開發遇到一個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
);