๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ORACLE/SQL

[Oracle] PIVOT, UNPIVOT ํ•จ์ˆ˜ ์‚ฌ์šฉ ํ–‰์—ด ์ „ํ™˜ :: ๋งˆ์ด์ž๋ชฝ

by ๐ŸŒปโ™š 2020. 3. 30.

ํ–‰์—ด ์ „ํ™˜

ORACLE SCOTT ๊ณ„์ • EMP ํ…Œ์ด๋ธ”์—์„œ 2๊ฐœ์˜ ์นผ๋Ÿผ์„ ์ด์šฉํ•˜์—ฌ GROUP BY ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ํ•˜๊ณ  ์‹ถ์€ ์ž‘์—…์€ GROUPINGํ•œ ์นผ๋Ÿผ์„ ๊ฐ€๋กœ ์„ธ๋กœ์ถ•์œผ๋กœ ๋‘๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. CASE ํ‘œํ˜„์‹์ด๋‚˜ DECODE ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

[Oracle] DECODE ํ•จ์ˆ˜ ์‚ฌ์šฉ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ ๊ฐ€๋กœ๋กœ ๋ณด๊ธฐ :: ๋งˆ์ด์ž๋ชฝ

DECODE ํ•จ์ˆ˜ DECODE ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋‚ด์˜ ์กฐ๊ฑด๋ฌธ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ํ™•์ธํ•  ๋Œ€์ƒ์„ ๋„ฃ์–ด์ฃผ๊ณ  2๋ฒˆ์งธ ์ธ์ž์— ํ™•์ธํ•  ๋Œ€์ƒ์˜ ์˜ˆ์ธก ๊ฐ’์ด ์žˆ์œผ๋ฉด 3๋ฒˆ์งธ ์ธ์ž๋ฅผ ์—†์œผ๋ฉด 4๋ฒˆ์งธ ์ธ์ž์˜ ๊ฐ’์„ ๋ฐ˜..

myjamong.tistory.com

 

DECODE ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ๊ณ„์† ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ค‘๋ณต๋˜๋Š” ์ž‘์—…์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์ž‘์—…์„ PIVOT ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๊ฒฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

PIVOT ์‚ฌ์šฉ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT
    *
FROM(
    SELECT
        DEPTNO
        ,JOB
        ,SAL
        FROM EMP
) PIVOT(
    AVG(SAL) FOR JOB IN (
        'ANALYST' AS ANALYST
        ,'CLERK' AS CLERK
        ,'MANAGER' AS MANAGER
        ,'PRESIDENT' AS PRESIDENT
        ,'SALESMAN' AS SALESMAN
    )
);

PIVOTํ•จ์ˆ˜๋Š” ํ–‰์„ ์—ด๋กœ ์ „ํ™˜ํ•ด์ค๋‹ˆ๋‹ค. ์‚ฌ์šฉํ• ๋•Œ ํ…Œ์ด๋ธ”์„ ๋ณด๊ณ  ๋ณ€ํ™˜์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ํ•„์š”ํ•œ ์นผ๋Ÿผ๋งŒ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. DEPTNO, JOB, SAL๋งŒ ์กฐํšŒํ•˜๋„๋ก ํ–ˆ๋Š”๋ฐ ๋งŒ์•ฝ EMPNO์ฒ˜๋Ÿผ ๋‹ค๋ฅธ ์นผ๋Ÿผ์„ ํ…Œ์ด๋ธ”์— ๋„ฃ์–ด๋ฒ„๋ฆฌ๋ฉด PIVOTํ• ๋•Œ ๊ฐ™์ด ๊ทธ๋ฃจํ•‘ํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

FOR์ ˆ ๋’ค์— ์ƒ๋‹จ ์นผ๋Ÿผ์œผ๋กœ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ ์นผ๋Ÿผ์„ ์œ„์น˜์‹œํ‚ค๊ณ  ์ถœ๋ ฅํ•  ๋ฐ์ดํ„ฐ๋ฅผ IN ํ•จ์ˆ˜ ์•ˆ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.

 

 

UNPIVOT ์‚ฌ์šฉ

PIVOT ํ•จ์ˆ˜๊ฐ€ ํ–‰์„ ์—ด๋กœ ์ „ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์˜€๋‹ค๋ฉด, UNPIVOT์€ ์—ด์„ ํ–‰์œผ๋กœ ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค. ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋จผ์ € CTAS ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ PIVOTํ•œ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด๋†“๊ฒ ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE  PIVOT_EMP
AS
SELECT
    *
FROM(
    SELECT
        DEPTNO
        ,JOB
        ,SAL
        FROM EMP
) PIVOT(
    AVG(SAL) FOR JOB IN (
        'ANALYST' AS ANALYST
        ,'CLERK' AS CLERK
        ,'MANAGER' AS MANAGER
        ,'PRESIDENT' AS PRESIDENT
        ,'SALESMAN' AS SALESMAN
    )
);

 

 

UNPIVOT ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ค์‹œ ๋˜๋Œ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
SELECT
    *
FROM PIVOT_EMP
UNPIVOT (
    SAL FOR JOB IN (
        ANALYST
        ,CLERK
        ,MANAGER
        ,PRESIDENT
        ,SALESMAN
    )
);

UNPIVOT์˜ ์‚ฌ์šฉ๋ฒ•๋„ PIVOT์ด๋ž‘ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค. ์ƒ๋‹จ์— ์žˆ๋˜ ์นผ๋Ÿผ๋ช…์„ INํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

PIVOT ์‚ฌ์šฉ์‹œ ์ฃผ์˜์‚ฌํ•ญ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DROP TABLE PIVOT_EMP;
CREATE TABLE  PIVOT_EMP
AS
SELECT
    *
FROM(
    SELECT
        DEPTNO
        ,JOB
        ,SAL
        FROM EMP
) PIVOT(
    AVG(SAL) FOR JOB IN ( --PIVOT์‹œ ALIAS๋ฅผ ์•ˆ
        'ANALYST'
        ,'CLERK'
        ,'MANAGER'
        ,'PRESIDENT'
        ,'SALESMAN'
    )
);
 

PIVOT์„ ์‚ฌ์šฉํ•ด์„œ ํ…Œ์ด๋ธ”์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด INํ•จ์ˆ˜์•ˆ์— ALIAS๋ฅผ ๊ผญ ๋„ฃ์–ด์„œ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์œ„์™€ ๊ฐ™์€ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  ์กฐํšŒํ•˜๋ฉด ์นผ๋Ÿผ์˜ ๋‚ด์šฉ์ด ์•„๋ž˜ ์ฒ˜๋Ÿผ ์ž‘์€ ๋”ฐ์˜ดํ‘œ์™€ ๊ฐ™์ด ์ƒ์„ฑ์ด ๋˜๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.

์กฐํšŒ์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ด๋ผ๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ, ๋งŒ์•ฝ ํ•ด๋‹น ํ…Œ์ด๋ธ”์„ UNPIVOTํ•˜๊ฒŒ ๋˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์นผ๋Ÿผ์— ๋Œ€ํ•œ ์ธ์‹์€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
SELECT
    *
FROM PIVOT_EMP
UNPIVOT (
    SAL FOR JOB IN (
        "'ANALYST'"
        ,"'CLERK'"
        ,"'MANAGER'"
        ,"'PRESIDENT'"
        ,"'SALESMAN'"
    )
);

ํฐ ๋”ฐ์˜ดํ‘œ๋ฅผ ์ด์šฉํ•ด์„œ ์ž‘์€ ๋”ฐ์˜ดํ‘œ๊นŒ์ง€ ์นผ๋Ÿผ์˜ ์ผ๋ถ€๋กœ ์ธ์‹ํ•˜๋ฉด UNPIVOT ์ž‘์—…์€ ๋ฌธ์ œ์—†์ด ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ž‘์€ ๋”ฐ์˜ดํ‘œ๊นŒ์ง€ ๋ฐ์ดํ„ฐ์— ๋“ค์–ด๊ฐ€ ๊ฒ€์ƒ‰์ž‘์—…์— ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋Œ“๊ธ€