728x90

오라클 Decode 함수는 자주 사용하는 함수이므로 익혀두면 개발하면서 유용하게 사용할 수 있다.

decode 함수는 MySQL 이나 MS-SQL 에서는 사용할 수 없다.

하나의 값만 비교하는 경우

  decode(값, 조건, true return, false return)

여러 값과 비교하는 경우

  decode(값, 조건1, true return1, 조건2, true return2, false return)


오라클에는 left, right 가 없다. 오직 substr 로 해결해야 한다.

테이블에 정의되지 않은 칼럼, 1월 ~ 12월은 쿼리상에서 만들어낸 데이터이다. 이를 보통 Entity 재정의라고 한다.


SELECT
상품,
sum(decode(substr(접수일자, 1, 6), '201401', 1, 0)) jan,
sum(decode(substr(접수일자, 1, 6), '201402', 1, 0)) feb,
sum(decode(substr(접수일자, 1, 6), '201403', 1, 0)) mar,
sum(decode(substr(접수일자, 1, 6), '201404', 1, 0)) apr,
sum(decode(substr(접수일자, 1, 6), '201405', 1, 0)) may,
sum(decode(substr(접수일자, 1, 6), '201406', 1, 0)) jun,
sum(decode(substr(접수일자, 1, 6), '201407', 1, 0)) jul,
sum(decode(substr(접수일자, 1, 6), '201408', 1, 0)) aug,
sum(decode(substr(접수일자, 1, 6), '201409', 1, 0)) sep,
sum(decode(substr(접수일자, 1, 6), '201410', 1, 0)) oct,
sum(decode(substr(접수일자, 1, 6), '201411', 1, 0)) nov,
sum(decode(substr(접수일자, 1, 6), '201412', 1, 0)) dec
FROM
(
 SELECT 접수번호, 접수일자, 접수시간, 처리일자, 처리시간, 상품, 유형, 접수채널, 접수자명, 처리자명
 FROM goods
)
where 유형 = '해제요청' AND 상담 LIKE '%해제%'
GROUP BY 상품;


http://www.gurubee.net/lecture/1028

에 DECODE 와 CASE 함수에 대해 설명하고 있다.



블로그 이미지

Link2Me

,