[Oracle] INLINE VIEW
INLINE VIEW
: 가상테이블. from절 안의 subquery. 쿼리문장의 수행이 끝나면 사라짐.
select *
from employees e, (select department_id dept_id, avg(salary) avg_sal
from employees
group by department_id) dept_avg
where e.department_id = dept_avg.dept_id
and e.salary > dept_avg.avg_sal;
pivot
: 행(세로) 데이터를 열(가로)로 변경하는 함수.
-pivot 함수 안에 그룹함수가 반드시 들어가야함
-inline view 안에 없는 컬럼은 pivot 안에 사용할 수 없음
-[문제73] 요일별 급여의 총액을 가로 방향으로 출력해주세요.
select *
from (select to_char(hire_date,'dy') day, salary
from employees)
pivot(sum(salary) for day in ('월' 월,'화' 화,'수' 수,'목' 목,'금' 금,'토' 토,'일' 일));
unpivot
: 열(가로)을 행(세로)로 변경하는 함수
-[문제74] 요일별 급여의 총액을 세로 방향으로 출력해주세요.
select *
from (select *
from (select to_char(hire_date,'dy') day, salary
from employees)
pivot(sum(salary) for day in ('월' 월,'화' 화,'수' 수,'목' 목,'금' 금,'토' 토,'일' 일)))
unpivot(급여총액 for 요일 in (월,화,수,목,금,토,일));
[문제68] 사원수가 가장 많은 부서이름, 도시, 인원수를 출력해주세요.
select d.department_name, l.city, e.cnt
from locations l, departments d, (select department_id, count(*) cnt
from employees
group by department_id
having count(*)=(select max(count(*))
from employees
group by department_id)) e
where e.department_id = d.department_id
and d.location_id = l.location_id;
[문제69] 입사가 가장 많은 요일에 입사한 사원들의 employee_id, last_name, 요일 출력해주세요.
select e.employee_id, e.last_name, o.day 요일
from employees e, (select to_char(hire_date,'day') day, count(*)
from employees
group by to_char(hire_date,'day')
having count(*) = (select max(count(*))
from employees
group by to_char(hire_date,'day'))) o
where o.day=to_char(e.hire_date,'day');
[문제70] 부서별로 인원수를 출력주세요.
10 20 30 40 50 60 70 80 90 100 110 부서가 없는 사원
1 2 6 1 45 5 1 34 3 6 2 1
select *
from (select department_id
from employees)
pivot (count(*) for department_id in (10,20,30,40,50,60,70,80,90,100,110,null "부서가 없는 사원"));
[문제71] 년도별 입사 인원수를 출력해주세요.
2001 2002 2003 2004 2005 2006 2007 2008
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1 7 6 10 29 24 19 11
select *
from (select to_char(hire_date,'yyyy') year
from employees)
pivot(count(*) for year in (2001,2002,2003,2004,2005,2006,2007,2008));
[문제72] 요일별 입사 인원수를 가로 방향으로 출력해주세요.
select *
from (select to_char(hire_date,'dy') day
from employees)
pivot(count(*) for day in ('월','화','수','목','금','토','일'));
[문제73] 요일별 급여의 총액을 가로 방향으로 출력해주세요.
select *
from (select to_char(hire_date,'dy') day, salary
from employees)
pivot(sum(salary) for day in ('월','화','수','목','금','토','일'));
[문제74] 요일별 급여의 총액을 세로 방향으로 출력해주세요.
select *
from (select *
from (select to_char(hire_date,'dy') day, salary
from employees)
pivot(sum(salary) for day in ('월' 월,'화' 화,'수' 수,'목' 목,'금' 금,'토' 토,'일' 일)))
unpivot(급여총액 for 요일 in (월,화,수,목,금,토,일));