Oracle 條件表示式 CASE DECODE

2021-09-16 18:45:48 字數 1267 閱讀 2337

在 sql 語句中使用if-then-else 邏輯使用兩種方法:

case 表示式:

case expr when comparison_expr1 then return_expr1

[when comparison_expr2 then return_expr2

when comparison_exprn then return_exprn

else else_expr]

end例子: 查詢部門號為 10, 20, 30 的員工資訊, 若部門號為 10, 則列印其工資的 1.1 倍, 20 號部門, 則列印其工資的 1.2 倍,

30 號部門列印其工資的 1.3 倍數。

select last_name, job_id, salary,

case job_id when 'it_prog' then 1.10*salary

when 'st_clerk' then 1.15*salary

when 'sa_rep' then 1.20*salary

else salary end "revised_salary"

from employees;

decode 函式:

decode(col|expression, search1, result1 ,

[, search2, result2,...,]

[, default])

例子1:

select last_name, job_id, salary,

decode(job_id, 'it_prog', 1.10*salary,

'st_clerk', 1.15*salary,

'sa_rep', 1.20*salary,

salary)

revised_salary

from employees;

例子2:

select last_name, salary,

decode (trunc(salary/2000, 0),

0, 0.00,

1, 0.09,

2, 0.20,

3, 0.30,

4, 0.40,

5, 0.42,

6, 0.44,

0.45) tax_rate

from employees

where department_id = 80;

條件分支 CASE表示式和DECODE函式

case表示式單條件 單列 來走分支 看下面的加薪語句 sql select last name,job id,salary,2 case job id when it prog then 1.10 salary 3 when st clerk then 1.15 salary 4 when sa ...

case表示式詳解

格式一 case 條件表示式 when 條件表示式結果1 then 語句段1 when 條件表示式結果2 then 語句段2 when 條件表示式結果n then 語句段n else 條件表示式結果 end 格式二 case when 條件表示式1 then 語句段1 when 條件表示式2 the...

oracle 每日一題 case表示式

原始出處 執行環境 sqlplus,serveroutput已開啟 我們正在建立乙個應用,使用者可以選擇他們的考題難度。他們可以選擇某種特定難度,也可以指定乙個範圍。我們用整數值來代表這些選項 1 beginner only 2 beginner intermediate 3 intermediat...