🤔 문제 분석
1. Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation.
2. The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
3. Note: Print NULL when there are no more names corresponding to an occupation.
💡 풀이
SELECT MAX(IF(OCCUPATION = "Doctor", NAME, NULL)) AS DOCTOR,
MAX(IF(OCCUPATION = "Professor", NAME, NULL)) AS Professor,
MAX(IF(OCCUPATION = "Singer", NAME, NULL)) AS Singer,
MAX(IF(OCCUPATION = "Actor", NAME, NULL)) AS Actor
FROM(
SELECT NAME, OCCUPATION, (ROW_NUMBER() OVER (PARTITION BY OCCUPATION ORDER BY NAME)) AS OCC
FROM OCCUPATIONS
) AS sub
GROUP BY OCC
'코딩테스트 > 해커랭크 SQL' 카테고리의 다른 글
[해커랭크/SQL] 36. Weather Observation Station 18 (0) | 2023.03.16 |
---|---|
[해커랭크/SQL] 35. Binary Tree Nodes (0) | 2023.03.14 |
[해커랭크/SQL] 33. Weather Observation Station 14 (0) | 2023.03.13 |
[해커랭크/SQL] 32. The PADS (concat, 문자열 합치기) (0) | 2023.03.13 |
[해커랭크/SQL] 31. Weather Observation Station 13 (0) | 2023.03.12 |
댓글