Pivot (동사)
(축을 중심으로) 회전하다[돌다]; 회전시키다[돌리다]https://en.dict.naver.com/#/entry/enko/6bd686da9dfb4993a00cdde4e613e777
select *
from dept;
select '부서명' as category,
min(decode(deptno, '10', dname, null)) as dept10,
min(decode(deptno, '20', dname, null)) as dept20,
min(decode(deptno, '30', dname, null)) as dept30,
min(decode(deptno, '40', dname, null)) as det40
from dept
union
select '부서위치' as category,
min(decode(deptno, '10', loc, null)) as dept10,
min(decode(deptno, '20', loc, null)) as dept20,
min(decode(deptno, '30', loc, null)) as dept30,
min(decode(deptno, '40', loc, null)) as det40
from dept;
select '부서명' as category,
min(decode(deptno, '10', dname, null)) as dept10,
min(decode(deptno, '20', dname, null)) as dept20,
min(decode(deptno, '30', dname, null)) as dept30,
min(decode(deptno, '40', dname, null)) as det40,
'부서위치' as category,
min(decode(deptno, '10', loc, null)) as dept10,
min(decode(deptno, '20', loc, null)) as dept20,
min(decode(deptno, '30', loc, null)) as dept30,
min(decode(deptno, '40', loc, null)) as det40
from dept;
-- 하나의 값만 비교
decode(검사할 컬럼|값, 비교할 값, 참일 경우 값, 거짓일 경우 값)
-- 여러 값 비교
decode(검사할 컬럼|값, 비교할 값, 참일 경우 값[, 비교할 값2, 참일 경우 값2 ], 거짓일 경우 값)
select '부서명' as category,
min((case when deptno=10 then dname else null end)) as dept10,
min((case when deptno=20 then dname else null end)) as dept20,
min((case when deptno=30 then dname else null end)) as dept30,
min((case when deptno=40 then dname else null end)) as dept40
from dept
union
select '부서위치' as category,
min((case when deptno=10 then loc else null end)) as dept10,
min((case when deptno=20 then loc else null end)) as dept20,
min((case when deptno=30 then loc else null end)) as dept30,
min((case when deptno=40 then loc else null end)) as dept40
from dept;
-- 하나의 값 비교
case when 조건식 then 참일 시 출력할 컬럼|값 else 그 외 출력할 값 end
-- 여러 값 비교
case when 조건식1 then 참 값1
when 조건식2 then 참 값2
else 그 외 값
end
-- 한 컬럼에 여러 값 대조
case 비교할 컬럼 when 값1 then 참 값1
when 값2 then 참 값2
else
end
select *
from (select '부서명'as category, dname, deptno from dept)
pivot(
max(dname) for deptno in (10,20,30,40)
)
union
select *
from (select '부서위치'as category, loc, deptno from dept)
pivot(
max(loc) for deptno in (10,20,30,40)
);
select *
from 조회할 테이블(혹은 인라인뷰)
pivot
(
집계함수 for 그룹핑할 컬럼 in (컬럼으로 사용할 값)
)