Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Hacker Rank] Occupations

์ธ” 2022. 9. 20. 11:21

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.

https://www.hackerrank.com


โ–  ๋ฌธ์ œ

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:

Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.

Sample Input

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