SELECT id
, SUM(CASE WHEN month = 'Jan' THEN revenue ELSE NULL END) AS Jan_Revenue
, SUM(CASE WHEN month = 'Feb' THEN revenue ELSE NULL END) AS Feb_Revenue
, SUM(CASE WHEN month = 'Mar' THEN revenue ELSE NULL END) AS Mar_Revenue
, SUM(CASE WHEN month = 'Apr' THEN revenue ELSE NULL END) AS Apr_Revenue
, SUM(CASE WHEN month = 'May' THEN revenue ELSE NULL END) AS May_Revenue
, SUM(CASE WHEN month = 'Jun' THEN revenue ELSE NULL END) AS Jun_Revenue
, SUM(CASE WHEN month = 'Jul' THEN revenue ELSE NULL END) AS Jul_Revenue
, SUM(CASE WHEN month = 'Aug' THEN revenue ELSE NULL END) AS Aug_Revenue
, SUM(CASE WHEN month = 'Sep' THEN revenue ELSE NULL END) AS Sep_Revenue
, SUM(CASE WHEN month = 'Oct' THEN revenue ELSE NULL END) AS Oct_Revenue
, SUM(CASE WHEN month = 'Nov' THEN revenue ELSE NULL END) AS Nov_Revenue
, SUM(CASE WHEN month = 'Dec' THEN revenue ELSE NULL END) AS Dec_Revenue
FROM Department
GROUP BY id
;
[ Group by - ์ ]
์ฝ๋
SELECT id
, CASE WHEN month='Jan' THEN revenue ELSE NULL END AS Jan_Revenue
, CASE WHEN month='Feb' THEN revenue ELSE NULL END AS Feb_Revenue
, CASE WHEN month='Mar' THEN revenue ELSE NULL END AS Mar_Revenue
FROM department
๊ฒฐ๊ณผ
id | Jan_Revenue | Feb_Revenue | Mar_Revenue |
---|---|---|---|
1 | 8000 | null | null |
2 | 9000 | null | null |
3 | null | 10000 | null |
1 | null | 7000 | null |
1 | null | null | 6000 |
[ Group by - ํ ]
์ฝ๋
SELECT id
, CASE WHEN month='Jan' THEN revenue ELSE NULL END AS Jan_Revenue
, CASE WHEN month='Feb' THEN revenue ELSE NULL END AS Feb_Revenue
, CASE WHEN month='Mar' THEN revenue ELSE NULL END AS Mar_Revenue
FROM department
GROUP BY id;
๊ฒฐ๊ณผ
id : 1 - Jan_Revenue ๊ฒฝ์ฐ
(8000, null, null) ๊ฐ์ด ๊ทธ๋ฃนํ ๋๋๋ฐ ๋งจ ์์ ์๋ 8000๋ง ํ๋ฉด์ ํ์๋ฉ๋๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก
id : 1 - Feb_Revenue ๊ฒฝ์ฐ
( null, 7000, null) ๊ฐ์ด ๊ทธ๋ฃนํ ๋๋ฉด์ ๋งจ ์์ null ๋ง ํ๋ฉด์ ํ์๋๋ ๊ฑฐ๋ผ๊ณ ๋ณผ ์ ์๊ฒ ๋ค์.
id | Jan_Revenue | Feb_Revenue | Mar_Revenue |
---|---|---|---|
1 | 8000 | null | null |
2 | 9000 | null | null |
3 | null | 10000 | null |
์ด๋ฐ ์์ผ๋ก ๊ฐ์ด ๋์ค๊ธฐ ๋๋ฌธ์ ์ง๊ณํจ์๋ฅผ ํตํด ์ค์ ์กด์ฌํ๋ ๊ฐ์ ์กฐ์ ํด ์ฃผ๋ ๋ฏ ํ๋ค์.