CASE 表達式是 SQL 里非常重要而且使用起來非常便利的技術,我們應該學會用它來描述條件分支。本節(jié)將通過行列轉換、已有數(shù)據(jù)重分組(分類)、與約束的結合使用、針對聚合結果的條件分支等例題,來介紹 CASE 表達式的用法。
case具有兩種格式。簡單case函數(shù)和case搜索函數(shù)。
--簡單case函數(shù)
case sex
when '1' then '男'
when '2' then '女'
else '其他' end
--case搜索函數(shù)
case when sex = '1' then '男'
when sex = '2' then '女'
else '其他' end
這兩種方式,可以實現(xiàn)相同的功能。簡單case函數(shù)的寫法相對比較簡潔,但是和case搜索函數(shù)相比,功能方面會有些限制,比如寫判定式。 還有一個需要注重的問題,case函數(shù)只返回第一個符合條件的值,剩下的case部分將會被自動忽略。
--比如說,下面這段sql,你永遠無法得到“第二類”這個結果
case when col_1 in ( 'a', 'b') then'第一類'
when col_1 in ('a') then '第二類'
else '其他' end
下面我們來看一下,使用case函數(shù)都能做些什么事情。
一 、已知數(shù)據(jù)按照另外一種方式進行分組,分析
有如下數(shù)據(jù):(為了看得更清楚,我并沒有使用國家代碼,而是直接用國家名作為primary key)
| 國家(country) | 人口 (population) |
|---|---|
| 中國 | 600 |
| 美國 | 100 |
| 加拿大 | 100 |
| 英國 | 200 |
| 法國 | 300 |
| 日本 | 250 |
| 德國 | 200 |
| 墨西哥 | 50 |
| 印度 | 250 |
根據(jù)這個國家人口數(shù)據(jù),統(tǒng)計亞洲和北美洲的人口數(shù)量。應該得到下面這個結果。
| 洲 | 人口 |
|---|---|
| 亞洲 | 1100 |
| 北美洲 | 250 |
| 其他 | 700 |
想要解決這個問題,你會怎么做?生成一個帶有洲code的view,是一個解決方法,但是這樣很難動態(tài)的改變統(tǒng)計的方式。
假如使用case函數(shù),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;
注:
在上面這個例子中,其實select 了兩個字段:sum(population), case country;
country 字段里面原來有很多值,有“中國,美國,日本,加拿大...”等等,select后的case when then else end其實就是相當于把country的值分為三類:"亞洲,北美洲,其他";
其實沒有from后面的case when語句,其結果集是這樣的,sum得到的是人口population的總數(shù),所以需要將population分組,就有了后面的group by。
同樣的,我們也可以用這個方法來判定工資的等級,并統(tǒng)計每一等級的人數(shù)。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_a
group 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語句完成不同條件的分組。
有如下數(shù)據(jù)
| 國家(country) | 性別(sex) | 人口(population) |
|---|---|---|
| 中國 | 1 | 340 |
| 中國 | 2 | 260 |
| 美國 | 1 | 45 |
| 美國 | 2 | 55 |
| 加拿大 | 1 | 51 |
| 加拿大 | 2 | 49 |
| 英國 | 1 | 40 |
| 英國 | 2 | 60 |
按照國家和性別進行分組,得出結果如下:
| 國家 | 男 | 女 |
|---|---|---|
| 中國 | 340 | 260 |
| 美國 | 45 | 55 |
| 加拿大 | 51 | 49 |
| 英國 | 40 | 60 |
普通情況下,用union也可以實現(xiàn)用一條語句進行查詢。但是那樣增加消耗(兩個select部分),而且sql語句會比較長。
下面是一個是用case函數(shù)來完成這個功能的例子
select country,
sum( case when sex = '1' then
population else 0 end), --男性人口
sum( case when sex = '2' then
population else 0 end) --女性人口
from table_a
group by country;
這樣我們使用select,完成對二維表的輸出形式,充分顯示了case函數(shù)的強大。
三、 在check中使用case函數(shù)
在check中使用case函數(shù)在很多情況下都是非常不錯的解決方法??赡苡泻芏嗳烁揪筒挥胏heck,那么我建議你在看過下面的例子之后也嘗試一下在sql中使用check。
下面我們來舉個例子
公司a,這個公司有個規(guī)定,女職員的工資必須高于1000塊。假如用check和case來表現(xiàn)的話,如下所示
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 )
女職員的條件倒是符合了,男職員就無法輸入了。
這兩種表達的差異體現(xiàn)在(蘊含式(conditional):記作 P → Q)和(邏輯與(logical product): P ∧ Q) 的區(qū)別。
文章源自下面這篇文章
https://blog.csdn.net/love_java_cc/article/details/52234415