Programming/Oracle SQL
[ Oracle ] Oracle sql case 문 예제
호IT
2021. 3. 15. 23:31
- 예제 문제 01 :
emp 에서 comm 값이 null 인 경우는 '해당 없음'으로, 아니면 원래 값이 출력되도록 empno,ename, sal, comm 을 출력하기 (nvl,decode,case 사용)
select empno, ename, sal,
case
when comm is null then '해당없음'
when comm=0 then 'zero'
when comm>=300 and comm<=500 then 'sales'
else to_char(comm,'999,999') end
as 상여금
from emp;
Oracle SQLl 에서 case 문은 switch-case 문과 거의 비슷합니다
- 예제 문제 02 :
job 이 'CLERK' 이면 sal*1.2 로 인상, MANAGER' 면 sal*1.5로 인상하고 나머지는 인상하지 않습니다.
empno, ename, job, sal, 인상된 sal을 출력하세요.
단, 소수점 첫째자리에서 반올림합니다.
select empno, ename, job, sal,
case job
when 'CLERK' then trunc(sal*1.2)
when 'MANAGER' then trunc(sal*1.5)
else sal end as "인상된 sal"
from emp;