oracle实验16:分支函数

实验16:分支的函数

case语句

语法:

CASE expr WHEN comparison_expr1THEN return_expr1
         [WHEN comparison_expr2 THENreturn_expr2
          WHEN comparison_exprn THENreturn_exprn
          ELSE else_expr]
END

 

case分支语句计算工资

SQL>  select ename,job,sal,
      case job when 'CLERK' then 1.10*sal
                   when 'SALESMAN' then 1.15*sal
                   when 'ANALYST' then 1.20*sal
      else sal
      end "revised_salary"
      from emp;

ENAME      JOB              SAL revised_salary                                 
---------- --------- ---------- --------------                                 
SMITH      CLERK            800            880                                 
ALLEN      SALESMAN        1600           1840                                 
WARD       SALESMAN        1250         1437.5                                 
JONES      MANAGER         2975           2975                                 
MARTIN     SALESMAN        1250         1437.5                                 
BLAKE      MANAGER         2850           2850                                 
CLARK      MANAGER         2450           2450                                 
KING       PRESIDENT       5000           5000                                 
TURNER     SALESMAN        1500           1725                                 
JAMES      CLERK            950           1045                                 
FORD       ANALYST         3000           3600                                    
MILLER     CLERK           1300           1430                                 

已选择12行。

 

decode函数

是oracle自定义的函数,其他数据库可能不支持。

语法:

DECODE(字段|表达式, 条件1,结果1
                  [,条件2,结果2…,]
                  [,缺省值])

 

decode语句计算工资

SQL> SELECT ENAME,JOB,SAL,
    DECODE(JOB,'CHERK' , 1.1*SAL
    ,'SALESMAN', 1.15*SAL,
    'ANALYST', 1.2*SAL, SAL)
    "REVISED_SALARY"
    FROM EMP;

ENAME      JOB              SAL REVISED_SALARY                                 
---------- --------- ---------- --------------                                 
SMITH      CLERK            800            800                                 
ALLEN      SALESMAN        1600           1840                                 
WARD       SALESMAN        1250         1437.5                                 
JONES      MANAGER         2975           2975                                 
MARTIN     SALESMAN        1250         1437.5                                 
BLAKE      MANAGER         2850           2850                                 
CLARK      MANAGER         2450           2450                                 
KING       PRESIDENT       5000           5000                                 
TURNER     SALESMAN        1500           1725                                 
JAMES      CLERK            950            950                                 
FORD       ANALYST         3000           3600                                   
MILLER     CLERK           1300           1300                                 

已选择12行。

 

求税率,每2000一个台阶,8000以上按40%。

SQL> select ename,sal,
    decode(trunc(sal/2000,0),
                        0,0.00,
                        1,0.09,
                        2,0.20,
                        3,0.30,
                          0.40) tax_rate
    from emp;

ENAME             SAL   TAX_RATE                                               
---------- ---------- ----------                                               
SMITH             800          0                                               
ALLEN            1600          0                                               
WARD             1250          0                                               
JONES            2975        .09                                               
MARTIN           1250          0                                               
BLAKE            2850        .09                                               
CLARK            2450        .09                                               
SCOTT            3000        .09                                               
KING              5000         .2                                               
TURNER           1500          0                                               
ADAMS            1100          0                                              
JAMES             950          0                                               
FORD             3000        .09                                               
MILLER           1300          0                                               

已选择14行。

 

case语句和decode函数都是单行函数,每一行都有一个返回值。

从oracle的角度来讲,decode更好,各个版本都支持。

从横向来说,case更好,他是国标,不同的数据库都认可。

 

ps:得知不用出差,可以安心的继续学习了,很感谢现在的工作可以给我一些时间学习,忍着肚子疼将单行函数的博客整理完成,继续加油!

 

返回目录  http://www.cnblogs.com/downpour/p/3155689.html

 

转载于:https://www.cnblogs.com/downpour/p/3155683.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值