[HackerRank] Occupations

JAMM·2023년 3월 18일
0

HackerRank

목록 보기
4/7
post-thumbnail

Problem


MySQL

/*
Enter your query here.
*/

/*
Step 1.
직업 별 이름을 알파벳 순서대로 row number를 기록하는 컬럼 추가
*/
with temp_01 as (
select
    NAME
    , OCCUPATION
    , row_number() over(partition by OCCUPATION order by NAME) as row_num
from OCCUPATIONS
)
/*
Step 2.
row number로 group by를 진행하여, 직업 별 이름의 알파벳 순의 row number level로 집합 수준 변경
이후, 직업 별 case when 문과 함께 pivot 진행
*/
select
    max(case when OCCUPATION = 'Doctor' then NAME else NULL end) as doctor_name
    , max(case when OCCUPATION = 'Professor' then NAME else NULL end) as professor_name
    , max(case when OCCUPATION = 'Singer' then NAME else NULL end) as singer_name
    , max(case when OCCUPATION = 'Actor' then NAME else NULL end) as actor_name
from temp_01
group by temp_01.row_num
;

Reference

HackerRank - Occupations

0개의 댓글