case...when 条件转换函数 case when有两种表现形式 1 case 变量 when 值1 then 结果1 when 值2 then 结果2 else ‘其它‘ end --else可以选择不要 示例: select ename,job,( case job when ‘MANAGER‘ then job when ‘CLERK‘ then job end) from emp; 2 case when 表达式1 then 结果1 when 表达式2 then 结果2 else ‘其它‘ end 示例: select ename,sal, case when deptno=10 then ‘会计部‘ when deptno=20 then ‘研究部‘ when deptno=30 then ‘销售部‘ else ‘其它部门‘ end from emp; --在case when中当前面条件筛选过后,后面是从前面剩下的数据中再抽取。 --以scott用户下面emp表为例。 --在进行第二中方法时需要考虑到顺序。 SQL> select t.*,case when t.sal>=3000 then ‘[3000‘ 2 when t.sal<3000 and t.sal>=2500 then ‘[2500-3000‘ 3 when t.sal<2500 and t.sal>=1500 then ‘[1500-2500‘ 4 else ‘<1500‘ end as sal_level 5 ,case when t.sal>=3000 then ‘[3000‘ 6 when t.sal>=2500 then ‘[2500-3000‘ 7 when t.sal>=1500 then ‘[1500-2500‘ 8 else ‘<1500‘ end as sal_level_test 9 from emp t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO SAL_LEVEL SAL_LEVEL_TEST ----- ---------- --------- ----- ----------- --------- --------- ------ ---------- -------------- 7369 SMITH CLERK 7902 1980/12/17 800.00 20 <1500 <1500 7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30 [1500-2500 [1500-2500 7521 WARD SALESMAN 7698 1981/2/22 1250.00 500.00 30 <1500 <1500 7566 JONES MANAGER 7839 1981/4/2 2975.00 20 [2500-3000 [2500-3000 7654 MARTIN SALESMAN 7698 1981/9/28 1250.00 1400.00 30 <1500 <1500 7698 BLAKE MANAGER 7839 1981/5/1 2850.00 30 [2500-3000 [2500-3000 7782 CLARK MANAGER 7839 1981/6/9 2450.00 10 [1500-2500 [1500-2500 7788 SCOTT ANALYST 7566 1987/4/19 3000.00 20 [3000 [3000 7839 KING PRESIDENT 1981/11/17 5000.00 10 [3000 [3000 7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30 [1500-2500 [1500-2500 7876 ADAMS CLERK 7788 1987/5/23 1100.00 20 <1500 <1500 7900 JAMES CLERK 7698 1981/12/3 950.00 30 <1500 <1500 7902 FORD ANALYST 7566 1981/12/3 3000.00 20 [3000 [3000 7934 MILLER CLERK 7782 1982/1/23 1300.00 10 <1500 <1500
原文地址:https://www.cnblogs.com/wangzihong/p/9058355.html
时间: 2024-10-19 07:33:00