case when语句的应用

case when语句运用
 1 select b.cur_branch,
 2 count(p.conttract_id), 
 3 sum(case when p.aff_date is null then p.payable_money else 0 end),
 4 sum(case when p.aff_date is null and p.fee_item ='租金' then p.payable_money else 0 end),
 5 sum(case when p.aff_date is null and p.fee_item ='电费' then p.payable_money else 0 end),
 6 sum(case when p.aff_date is not null then 1 else 0 end),
 7 sum(case when p.aff_date is null then 1 else 0 end)
 8 from WG_PJHSB p, wg_jzmb b 
 9 where p.station_id = b.station_code
10 group by b.cur_branch
11 order by b.cur_branch

运用1:sum(case when p.aff_date is null then p.payable_money else 0 end)

累加票据金额

运用2:sum(case when 条件语句l then 1 else 0 end)

累加计数使用。

 

posted @ 2016-11-08 19:43  callmehn  阅读(183)  评论(0编辑  收藏  举报