DECODE
조건에 따라 다양한 선택 ( if문 ,switch 문과 비슷함 )
사용법
select decode(컬럼A, 대상B, 출력X,
대상C, 출력Y,
디폴트 출력(defalt)Z)
from 테이블이름;
예제
select ename,dno,
decode(dno,10,'ACCOUNTING',
20,'RESERCH',
30,'SALES',
40,'OPERRATING',
'DEFALT') as dname
from employee
order by dno;
한글도 사용 가능
select ename 사원명 ,dno 부서번호,
decode(dno,10,'회계',
20,'연구',
30,'판매',
40,'관리',
'DEFALT') as dname
from employee
order by dno;
CASE
select ename 사원명,dno 부서번호,
case when dno=10 then 'ACCOUNTING'
when dno=20 then 'RESERCH'
when dno=30 then 'SALES'
when dno=40 then 'OPERRATING'
else 'DEFALT'
end as 부서명
from employee
order by dno;
한글 사용
select ename 사원명,dno 부서번호,
case when dno=10 then '회계'
when dno=20 then '연구'
when dno=30 then '판매'
when dno=40 then '관리'
else '대기'
end as 부서명
from employee
order by dno;
여러개를 함께 쓸수도 있다.
select ename 사원명,dno 부서번호,
case when dno=10 then '회계'
when dno=20 then '연구'
when dno=30 then '판매'
when dno=40 then '관리'
else '대기중'
end as 부서명,
case when 4000 <= salary then '임원'
when 3000 <= salary and salary <4000 then '부장'
when 2000 <= salary and salary <3000 then '대리'
when 1000 <= salary and salary <2000 then '사원'
else '인턴'
end 직급,
salary 급여
from employee
order by salary desc;
DECODE 와 CASE 차이점
DECODE :
함수
단순 등가비교( = )
조건값과 같은지의 여부만 확인
NULL과 NULL의 비교 >> TRUE
CASE :
STATEMENT
복잡한 조건( < , > 등)
WHEN 이후에 조건문을 작성가능
NULL과 NULL의 비교 >> FALSE
CASE문의 길이가 보통 더 길다.
실행속도는 간단한 수준에서는 크게 차이나지 않음
하지만 어느정도 크기가 커지면 CASE문이 더 빠르다고 한다.
간단한 쿼리에서는 DECODE를 , 복잡한 조건을 걸 때 등에는 CASE
'SQL > ORACLE SQL' 카테고리의 다른 글
SUB QUERY : 서브 쿼리 SELECT (0) | 2022.11.04 |
---|---|
JOIN : 조인 (0) | 2022.11.03 |
ORACLE : 날짜 함수, 형 변환 함수 (0) | 2022.11.02 |
ORACLE : 문자 처리 함수, 숫자 함수 (0) | 2022.11.02 |
정렬 : ORDER BY (0) | 2022.11.01 |