一个语句整合了case when的精妙用法

统计这个玩意有时确实很烦人,但有时又很可爱,有时你只要亲切的动用case when语句,这个语句将无比强大。当然这个是在oracle强力的支持下:

select b.no,b.org_no,o.name,
   ( case when (o.upper1 is null) then ''    when (o.upper2 is null) then o.no    when (o.upper3 is null) then o.upper1    else o.upper2 en d ) as up,org.name,
    sum(case when( h.transtype='CWD'  ) then 1 else 0 end),
    sum(case when( h.transtype='CWD'  and h.transamount is not null) then h.transamount else '0'       end),
    sum(case when( h.transtype='CDP'  and h.transamount is not null) then 1 else 0 end),
    sum(case when( h.transtype='CDP'  and h.transamount is not null) then h.transamount else '0' end),
    sum(case when( h.transtype='TFR'  and h.transamount is not null) then 1 else 0 end),
    sum(case when( h.transtype='TFR'  and h.transamount is not null ) then h.transamount else '0' end),
    sum(case when( h.transtype='FUL' or h.transtype='FUB' or h.transtype='FUR' ) then 1 else 0 end),
    sum(case when( h.transtype='FUL' or h.transtype='FUB' or h.transtype='FUR' and h.subamount is not null) then h.subamount else '0'  end),
    sum(case when( (h.transtype='BTB' or h.transtype='BTF') and h.banktfrbilltype ='13') then 1 else 0 end),
    sum(case when( (h.transtype='BTB' or h.transtype='BTF') and h.banktfrbilltype ='13' and h.banktframount is not null ) then h.banktfra mount els  e '0' end),
    sum(case when( (h.transtype='BTB' or h.transtype='BTF') and h.banktfrbilltype ='14' ) then 1 else 0 end),
    sum(case when( (h.transtype='BTB' or h.transtype='BTF') and h.banktfrbilltype ='14' and h.banktframount is not null ) then h.banktfra mount els  e '0' end),
    sum(case when( h.transtype='TTB' or h.transtype='TTF') then 1 else 0 end),
    sum(case when( (h.transtype='TTB' or h.transtype='TTF') and h.thirdpartyamount is not null) then h.thirdpartyamount else '0' end),
    sum(case when( h.transtype='YDP'  ) then 1 else 0 end),sum(case when( h.transtype='YDP' and h.paymentamount is not null  ) then  h.payment amount else '0' end),
    sum(case when( h.transtype='LTP'  ) then 1 else 0 end),sum(case when( h.transtype='LTP' and h.paymentamount is not null  ) then  h.paymenta mount else '0' end),
    sum(case when( h.transtype='DXP'  ) then 1 else 0 end),sum(case when( h.transtype='DXP' and h.paymentamount is not null  ) then  h.payment amount else '0' end),
    sum(case when( h.transtype='MSP'  ) then 1 else 0 end)
    from hist_transtable h ,dev_base_info b,org_table o,(select no,name from org_table ) org
    where  h.devno = b.no and  substr(h.transtime,0,8) ='20091022'  and org.no(+) = ( case when (o.upper1 is null) then ''    when (o.uppe r2 is null) t     hen o.no    when (o.upper3 is null) then o.upper1    else o.upper2 end )  and b.org_no = o.no and (o.no ='49999 99'  or ( o.upper1 ='4999     999' or   o.upper2='4999999' or o.upper3='4999999' or o.upper4='4999999' or o.upper5='499999 9' )) 
 group by  ( case when (o.upper1 is null) then ''    when (o.upper2 is null) then o.no    when (o.upper3 is null) then o.upper1    else o. upper2 end ) ,org.name,b.org_no,o.name,b.no
    order by up asc,b.org_no asc,b.no asc

 

也许看着这么长的语句看着心烦,但是你会发现里面浓缩了很多case when精妙的语法.

这段sql语句里面含有case when 语句的基本应用,子查询语句,case when的表达式的与表字段的外连接.....

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值