응용 SoftWare/Oracle

[내장 함수] CASE

Hyun CHO 2017. 1. 11. 10:51

-- CASE 문장 : IF ~ ELSE문과 같은 기능

select * from empcopy;


-- EMPCOPY 테이블에서 부서코드를 10-ACCOUNTING, 20-RESEARCH, 30-SALES, 그 외-ETC로 선택

select ename, deptno, case deptno

 when 10 then 'accounting'

 when 20 then 'research'

 when 30 then 'sales'

 else 'etc'

end

from empcopy order by deptno asc; -- deptno 기준 오름차순 정렬 / asc 생략 가능


-- EMPCOPY 테이블에서 급여가 0~1000불 까지는 1호봉, 1000~2000불 까지는 2호봉, 2000~3000불 까지는 3호봉, 3000~4000불 까지는 4호봉, 4000불 이상은 5호봉

select ename, sal, case

 when sal>=0 and sal<=1000 then '1호봉'

 when sal between 1000 and 2000 then '2호봉'

 when sal between 2000 and 3000 then '3호봉'

 when sal between 3000 and 4000 then '4호봉'

 when sal>=4000 then '5호봉'

end

from empcopy;