728x90
※ The OCCUPATIONS table is described as follows:
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
■ [Advanced Select] Occupations
https://www.hackerrank.com/challenges/occupations/problem?isFullScreen=true
Q.
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.
A.
WITH D_NM AS (
SELECT ROWNUM AS RN, NAME
FROM (
SELECT NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Doctor'
ORDER BY 1 -- NAME
)
), P_NM AS (
SELECT ROWNUM AS RN, NAME
FROM (
SELECT NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Professor'
ORDER BY 1
)
), S_NM AS (
SELECT ROWNUM AS RN, NAME
FROM (
SELECT NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Singer'
ORDER BY 1
)
), A_NM AS (
SELECT ROWNUM AS RN, NAME
FROM (
SELECT NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Actor'
ORDER BY 1
)
)
SELECT NVL(D_NM.NAME,'NULL') AS DOCTORS
,NVL(P_NM.NAME,'NULL') AS PROFESSOR
,NVL(S_NM.NAME,'NULL') AS SINGERS
,NVL(A_NM.NAME,'NULL') AS ACTORS
FROM D_NM FULL JOIN P_NM USING(RN)
FULL JOIN S_NM USING(RN)
FULL JOIN A_NM USING(RN);
WITH D_NM AS (
SELECT ROW_NUMBER() OVER(ORDER BY NAME) AS RN, NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Doctor'
), P_NM AS (
SELECT ROW_NUMBER() OVER(ORDER BY NAME) AS RN, NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Professor'
), S_NM AS (
SELECT ROW_NUMBER() OVER(ORDER BY NAME) AS RN, NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Singer'
), A_NM AS (
SELECT ROW_NUMBER() OVER(ORDER BY NAME) AS RN, NAME
FROM OCCUPATIONS
WHERE OCCUPATION = 'Actor'
)
SELECT NVL(D_NM.NAME,'NULL') AS DOCTORS
,NVL(P_NM.NAME,'NULL') AS PROFESSOR
,NVL(S_NM.NAME,'NULL') AS SINGERS
,NVL(A_NM.NAME,'NULL') AS ACTORS
FROM D_NM FULL JOIN P_NM USING(RN)
FULL JOIN S_NM USING(RN)
FULL JOIN A_NM USING(RN);
반응형
'코딩 문제 풀기 ( Algorithm problem solving ) > 해커랭크 ( HackerRank )' 카테고리의 다른 글
[HackerRank][SQL(Oracle)] 15 Days of Learning SQL (0) | 2023.04.05 |
---|---|
[HackerRank][SQL(Oracle)] Interviews (0) | 2023.04.05 |
[HackerRank][SQL(Oracle)] Challenges (0) | 2023.04.01 |
[HackerRank][SQL(Oracle)] The PADS (0) | 2023.04.01 |
[HackerRank][SQL(Oracle)] Ollivander's Inventory (0) | 2023.03.29 |