-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp206.sql
More file actions
67 lines (59 loc) · 1.55 KB
/
Copy pathp206.sql
File metadata and controls
67 lines (59 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--LISTAGG 함수
select ename
from emp
where deptno = 10;
select deptno, ename
from emp
group by deptno, ename
order by deptno;
select deptno, listagg(ename, ', ')within group(order by sal desc) as enames
from emp
group by deptno;
--pivot, unpivot 함수
select deptno, job, max(sal)
from emp
group by deptno, job
order by deptno, job;
select *
from (select deptno, job, sal from emp)
pivot(max(sal) for deptno in (10, 20, 30))
order by job;
select *
from (select deptno, job, sal from emp)
pivot(max(sal) for deptno in (10, 20, 30))
order by job;
select
job,
nvl("10", 0) as "10",
nvl("20", 0) as "20",
nvl("30", 0) as "30"
from (select deptno, job, sal from emp)
pivot (
max(sal) for deptno in (10, 20, 30)
)
order by job;
--decode문을 활용하여 pivot 함수와 같은 출력 구현
select deptno,
max(decode(job, 'CLERK', sal)) as "CLERK",
max(decode(job, 'SALESMAN', sal)) as "SALESMAN",
max(decode(job, 'PRESIDENT', sal)) as "PRESIDENT",
max(decode(job, 'MANAGER', sal)) as "MANAGER",
max(decode(job, 'ANALYSY', sal)) as "ANALYSY"
from emp
group by deptno
order by deptno;
--unppivot 함수를 사용하여 열로 구분된 그룹을 행으로 출력
select *
from (
select deptno,
max(decode(job, 'CLERK', sal)) as "CLERK",
max(decode(job, 'SALESMAN', sal)) as "SALESMAN",
max(decode(job, 'PRESIDENT', sal)) as "PRESIDENT",
max(decode(job, 'MANAGER', sal)) as "MANAGER",
max(decode(job, 'ANALYST', sal)) as "ANALYST"
from emp
group by deptno
order by deptno
)
unpivot(sal for job in(CLERK, SALESMAN, PRESIDENT, MANAGER, ANALYST))
order by deptno, job;