코딩 연습/SQL
[Hacker Rank] Occupations
썬2
2021. 4. 4. 22:40
문제 풀이
select max(case when occupation='Doctor' then name end),
max(case when occupation='Professor' then name end),
max(case when occupation='Singer' then name end),
max(case when occupation='Actor' then name end)
from (select *, row_number() over (partition by occupation
order by name) rn
from occupations) t
group by rn;
포인트
1. row_number~로 이름에 숫자 붙기기
2. max를 써서 NULL은 맨 마지막으로 처리하기
느낀점
지금까지 풀어본 SQL문제 중 top2안에 들 정도로 어려웠다.
다음에 풀 때는 참고하지 않고 꼭 혼자 풀어봐야겠다. (pivot 테이블로 만드는거 신기해)
3, 4번 풀어야지