sql之case when 用法

贴两个例子:

由左图得到右图,可用如下sql:

          

  1. select (case
  2.          when chnl_type_id = '100' then
  3.           '自办营业厅'
  4.          when chnl_type_id = '200' then
  5.           '合作厅'
  6.          when chnl_type_id = '300' then
  7.           '外呼'
  8.          when chnl_type_id = '1' then
  9.           '10086人工'
  10.          when chnl_type_id = '2' then
  11.           '10086自动'
  12.          when chnl_type_id = '6' then
  13.           '短信营业厅'
  14.          when chnl_type_id = '5' then
  15.           '网上营业厅'
  16.          when chnl_type_id = '3' then
  17.           'WAP营业厅'
  18.          when chnl_type_id = '4' then
  19.           '自助终端'
  20.          else
  21.           ''
  22.        end) as chnl_type_id,
  23.        sum(biz_accept_cnt) as cnt1
  24.   from masacb.tb_cb_rs_echnl_key_serv_m t
  25.  where chnl_type_id in ('1''2''3''4''5''6''100''200''300')
  26.    and t.statis_month between 200801 and 200810
  27.    and t.area_id like '%'
  28.    and t.brand_id like '%'
  29.    and t.cust_type_id like '%'
  30.    and t.cust_lvl_id like '%'
  31.    and t.chg_mode like '1'
  32.    and t.biz_accept_type_id like '%'
  33.    and t.serv_type_id like '%'
  34.  group by t.chnl_type_id

例子2:

 

 

 

 

 

  1. select CUST_LVL_ID,
  2.        --biz_accept_type_id,
  3.        sum(case
  4.              when chnl_type_id = '100' then
  5.               cnt1
  6.            end) as 自办营业厅,
  7.        sum(case
  8.              when chnl_type_id = '200' then
  9.               cnt1
  10.            end) as 合作厅,
  11.        sum(case
  12.              when chnl_type_id = '300' then
  13.               cnt1
  14.            end) as 外呼,
  15.        sum(case
  16.              when chnl_type_id = '1' then
  17.               cnt1
  18.            end) as 人工,
  19.        sum(case
  20.              when chnl_type_id = '2' then
  21.               cnt1
  22.            end) as 自动,
  23.        sum(case
  24.              when chnl_type_id = '3' then
  25.               cnt1
  26.            end) as WAP营业厅,
  27.        sum(case
  28.              when chnl_type_id = '4' then
  29.               cnt1
  30.            end) as 自助终端,
  31.        sum(case
  32.              when chnl_type_id = '5' then
  33.               cnt1
  34.            end) as 网上营业厅,
  35.        sum(case
  36.              when chnl_type_id = '6' then
  37.               cnt1
  38.            end) as WAP营业厅
  39.   from (select (case
  40.                  when CUST_LVL_ID = '1' then
  41.                   '0元'
  42.                  when CUST_LVL_ID = '2' then
  43.                   '0-50元'
  44.                  when CUST_LVL_ID = '3' then
  45.                   '50-100元'
  46.                  when CUST_LVL_ID = '4' then
  47.                   '100-150元'
  48.                  when CUST_LVL_ID = '5' then
  49.                   '150-200元'
  50.                  when CUST_LVL_ID = '6' then
  51.                   '200元以上'
  52.                  else
  53.                   ''
  54.                end) as CUST_LVL_ID,
  55.                chnl_type_id,
  56.                sum(biz_accept_succ_cnt) as cnt1
  57.           from masacb.tb_cb_rs_echnl_key_serv_m t
  58.          where chnl_type_id in
  59.                ('1''2''3''4''5''6''100''200''300')
  60.          group by t.CUST_LVL_ID, t.chnl_type_id
  61.          order by t.cust_lvl_id)
  62.  group by CUST_LVL_ID

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值