sql之case when 用法

來源:互聯網
上載者:User

貼兩個例子:

由左圖得到右圖,可用如下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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.