๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋ HackerRank ์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://www.hackerrank.com/challenges/occupations/problem
Occupations | HackerRank
Pivot the Occupation column so the Name of each person in OCCUPATIONS is displayed underneath their respective Occupation.
www.hackerrank.com
Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
Note: Print NULL when there are no more names corresponding to an occupation.
Input Format
The OCCUPATIONS table is described as follows:
![](https://blog.kakaocdn.net/dn/c94eYC/btrMvuakm9p/3IhcCRXDAKgK1ZnPd2Iyj1/img.png)
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
Sample Input
![](https://blog.kakaocdn.net/dn/nHpar/btrMxUswP03/RaCk1Xt79Zk02c5BukQvH1/img.png)
Sample Output
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
Explanation
The first column is an alphabetically ordered list of Doctor names.
The second column is an alphabetically ordered list of Professor names.
The third column is an alphabetically ordered list of Singer names.
The fourth column is an alphabetically ordered list of Actor names.
The empty cell data for columns with less than the maximum number of names per occupation (in this case, the Professor and Actor columns) are filled with NULL values.
โ ํ์ด
Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation.
> ์ง์ ์ ๋ฐ๋ผ ์ด๋ฆ ์ํ๋ฒณ ์์ผ๋ก ์ ๋ ฌ๋๋๋ก OCCUPATIONS ํ ์ด๋ธ ์ปฌ๋ผ ํผ๋ดํด๋ผ โ
The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
> ์ถ๋ ฅ ์ปฌ๋ผ์ ์ง์ ์ด์ด์ผ ํ๋ค. โก
PIVOT FUNCTION TAGGING
CASE WHEN
DECODE
IF , ์ง๊ณํจ์ (* IF์ ์ง๊ณํจ์๋ก ํผ๋ดํ ๋ Key๋ ROW_NUM()
ํ์ด1> MySQL
SELECT MIN(CASE WHEN occupation = 'Doctor' THEN name END) Doctor,
MIN(CASE WHEN occupation = 'Professor' THEN name END) Professor,
MIN(CASE WHEN occupation = 'Singer' THEN name END) Singer,
MIN(CASE WHEN occupation = 'Actor' THEN name END) Actor
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY name) rnk
FROM OCCUPATIONS) A
GROUP BY rnk
ORDER BY rnk;
๋จ, SELECT์ ์ ์ฌ์ฉ๋๋ ์ง๊ณ ํจ์๋ MIN, MAX ๋ ์๊ด์ด ์๋ค.
# from์ ์๋ธ์ฟผ๋ฆฌ
SELECT *, ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY name) rnk
FROM OCCUPATIONS;
> from์ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ๋ฐ๋ก ์ถ๋ ฅํ๋ฉด
์์ ๊ฐ์ด ์ง์ (Occupation)๋ณ ์ด๋ฆ์์ผ๋ก ์ ๋ ฌ๋ ํ ์ ๋ ฌ ๋ฒํธ(rnk)๊ฐ ๋ถ๋๋ค.
๋ฉ์ธ ์ฟผ๋ฆฌ๋ ์ด ๊ฒฐ๊ณผ์ ๋ํด์ rnk๋ก ๊ทธ๋ฃนํํ๋๋ฐ ๊ทธ๋ฃนํ ํ๊ณ ๋๋ฉด ๊ฐ rnk๋น ์ง์ ๋ณ ์ธ์์ 1๋ช ๋ฐ์ ์กด์ฌํ์ง์๊ฒ ๋๋ค. ๋ฐ๋ผ์ SELECT์ ์์ CASE WHEN์ผ๋ก ์ง์ ์กฐ๊ฑด์ ์ฃผ๊ณ ์ถ์ถํ ๋, MIN, MAX์ค ์ด๋ ๊ฒ์ ์จ๋ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ์ถํ ์ ์๋ค.
ํ์ด2> Oracle
SELECT MIN(DECODE(OCCUPATION, 'Doctor', Name)) Doctor
, MIN(DECODE(OCCUPATION, 'Professor', Name)) Professor
, MIN(DECODE(OCCUPATION, 'Singer', Name)) Singer
, MIN(DECODE(OCCUPATION, 'Actor', Name)) Actor
FROM (
SELECT OCCUPATION, NAME
, ROW_NUMBER() OVER(PARTITION BY OCCUPATION ORDER BY NAME) rnk
FROM OCCUPATIONS
) A
GROUP BY rnk
ORDER BY 1, 2, 3, 4;
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Hacker Rank] Contest Leaderboard (0) | 2022.09.20 |
---|---|
[Hacker Rank] SQL Project Planning (0) | 2022.09.20 |
[Hacker Rank] Draw The Triangle 2 (1) | 2022.09.20 |
[Hacker Rank] Draw The Triangle 1 (0) | 2022.09.13 |
[๊ณผ์ ] Pythob2022 #03 ์ฐ์ต๋ฌธ์ (0) | 2022.08.27 |