T SQL朝花夕拾 六 Case語句

2021-09-07 21:05:40 字數 3772 閱讀 5251

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

--簡單case函式

case ***

when

'1'then

'男'when

'2'then

'女'else

'其他'

end--case搜尋函式

case

when *** = '1'

then

'男'when *** = '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

'其他'

endfrom table_a

group

bycase 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_a

group

bycase

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)

性別(***)

人口 (population)中國1

340中國

2260美國1

45美國255

加拿大1

51加拿大249

英國140英國260

按照國家和性別進行分組,得出結果如下 國家

男女中國340

260美國

4555

加拿大51

49英國

4060

普通情況下,用union也可以實現用一條語句進行查詢。但是那樣增加消耗(兩個select部分),而且sql語句會比較長。

下面是乙個是用case函式來完成這個功能的例子

select country,

sum( case

when *** = '1'

then

population else 0 end), --男性人口

sum( case

when *** = '2'

then

population else 0 end) --女性人口

from table_a

group

by country;

這樣我們使用select,完成對二維表的輸出形式,充分顯示了case函式的強大。

三,在check中使用case函式。

在check中使用case函式在很多情況下都是非常不錯的解決方法。可能有很多人根本就不用check,那麼我建議你在看過下面的例子之後也嘗試一下在 sql中使用check。

下面我們來舉個例子

公司a,這個公司有個規定,女職員的工資必須高於1000塊。如果用check和case來表現的話,如下所示

constraint check_salary check

( case

when *** = '2'

then

case

when salary > 1000

then 1 else 0 end

else 1 end = 1 )

如果單純使用check,如下所示

constraint check_salary check

( *** = '2'

and salary > 1000 )

女職員的條件倒是符合了,男職員就無法輸入了。

T SQL中Case的用法

case語句是條件判斷語句的一種,可以完成比if語句更強的判斷,可以解決if語句中巢狀過多的問題。語法 case when 條件a then 結果a when 條件b 結果b else 結果n end 和decode 不同的是,decode 只能針對固定的值,而 case 可以用不是固定值,需是乙個...

T SQL中case語句的兩種寫法及區別

t sql中的case語句相信大家一定不陌生,但是它有2種寫法,如下 寫法一 case 變數 when 值1 then.when 值2 then.else end寫法二 case when 邏輯表示式 then true的情況 else false的情況 end如果是二叉分支,筆者建議寫法二 因為,...

case分支語句

case分支語句 匹配執行的方式,針對的變數預先存在的值,判斷該變數 實際取值是否和預設的值相匹配,如果匹配,就執行相應的操作 如果不匹配,就執行預先設定好的預設操作 語法結構 case 變數值 in 模式1 命令序列1 模式2 命令序列2 預設命令序列 esac bin bash read p 請...