SQl CASE 語句的嵌套使用方式

來源:互聯網
上載者:User

case具有兩種格式。簡單case函數和case搜尋函數。 
1.簡單case函數

 

case sex when ’1’ then ’男’ when ’2’ then ’女’else ’其他’ end

 

2. case搜尋函數

 

case when sex = ’1’ then ’男’when sex = ’2’ then ’女’else ’其他’ end

 

這兩種方式,可以實現相同的功能。簡單case函數的寫法相對比較簡潔,但是和case搜尋函數相比,功能方面會有些限制,比如寫判斷式。 還有一個需要注意的問題,case函數只返回第一個合格值,剩下的case部分將會被自動忽略。比如說,下面這段sql,你永遠無法得到“第二類”這個結果

 

case when col_1 in ( ’a’, ’b’) then ’第一類’when col_1 in (’a’)  then ’第二類’else’其他’ end

 

 

下面我們來看一下,使用case函數都能做些什麼事情。 

一,已知資料按照另外一種方式進行分組,分析。 有如下資料:(為了看得更清楚,我並沒有使用國家代碼,而是直接用國家名作為primary key) 

國家(country)人口(population)

中國                   600

美國                    100

加拿大                 100

英國                    200

法國                    300

日本                    250

德國                    200

墨西哥                 50

印度                    250

 

根據這個國家人口資料,統計亞洲和北美洲的人口數量。應該得到下面這個結果。 

洲               人口

亞洲            1100

北美洲          250

其他             700

 

想要解決這個問題,你會怎麼做?產生一個帶有洲code的view,是一個解決方案,但是這樣很難動態改變統計的方式。 如果使用case函數,sql代碼如下: 

select     sum(population),        case country                when ’中國’     then ’亞洲’                when ’印度’     then ’亞洲’                when ’日本’     then ’亞洲’                when ’美國’     then ’北美洲’                when ’加拿大’  then ’北美洲’                when ’墨西哥’  then ’北美洲’        else ’其他’ end from       table_a group by case country   when ’中國’     then ’亞洲’                when ’印度’     then ’亞洲’                when ’日本’     then ’亞洲’                when ’美國’     then ’北美洲’                when ’加拿大’  then ’北美洲’                when ’墨西哥’  then ’北美洲’        else ’其他’ end;同樣的,我們也可以用這個方法來判斷工資的等級,並統計每一等級的人數。sql代碼如下; select        case when salary <= 500 then ’1’             when salary > 500 and salary <= 600  then ’2’             when salary > 600 and salary <= 800  then ’3’             when salary > 800 and salary <= 1000 then ’4’        else null end salary_class,           count(*)from       table_agroup by        case when salary <= 500 then ’1’             when salary > 500 and salary <= 600  then ’2’             when salary > 600 and salary <= 800  then ’3’             when salary > 800 and salary <= 1000 then ’4’        else null end;二,用一個sql陳述式完成不同條件的分組。 有如下資料 國家(country)性別(sex)人口(population)中國1340中國2260美國145美國255加拿大151加拿大249英國140英國260按照國家和性別進行分組,得出結果如下 國家男女中國340260美國4555加拿大5149英國4060普通情況下,用union也可以實現用一條語句進行查詢。但是那樣增加消耗(兩個select部分),而且sql語句會比較長。 下面是一個是用case函數來完成這個功能的例子 select country,          sum( case when sex = ’1’ then                          population else 0 end),  --男性人口          sum( case when sex = ’2’ then                          population else 0 end)   --女性人口from     table_agroup by country;這樣我們使用select,完成對二維表的輸出形式,充分顯示了case函數的強大。 三,在check中使用case函數。 在check中使用case函數在很多情況下都是非常不錯的解決方案。可能有很多人根本就不用check,那麼我建議你在看過下面的例子之後也嘗試一下在sql中使用check。 下面我們來舉個例子 公司a,這個公司有個規定,女職員的工資必須高於1000塊。如果用check和case來表現的話,如下所示 constraint check_salary check              ( case when sex = ’2’                  then case when salary > 1000                        then 1 else 0 end                  else 1 end = 1 )如果單純使用check,如下所示 constraint check_salary check              ( sex = ’2’ and salary > 1000 )女職員的條件倒是符合了,男職員就無法輸入了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.