Studying data
[HackerRank] Advanced Select > Occupations 본문
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.
[MySQL Solution 1]
set @r1=0, @r2=0, @r3=0, @r4=0;
select min(Doctor), min(Professor), min(Singer), min(Actor) -- min 대신 max도 가능
from(
select case when Occupation='Doctor' then (@r1:=@r1+1)
when Occupation='Professor' then (@r2:=@r2+1)
when Occupation='Singer' then (@r3:=@r3+1)
when Occupation='Actor' then (@r4:=@r4+1) end as RowNumber,
case when Occupation='Doctor' then Name end as Doctor,
case when Occupation='Professor' then Name end as Professor,
case when Occupation='Singer' then Name end as Singer,
case when Occupation='Actor' then Name end as Actor
from OCCUPATIONS
order by Name
) temp
group by RowNumber;
참고:
https://www.hackerrank.com/challenges/occupations/forum/comments/731399
https://stackoverflow.com/questions/1009954/mysql-variable-vs-variable-whats-the-difference
[MySQL Solution 2]
SELECT min(Doctor), min(Professor), min(Singer), min(Actor)
FROM (SELECT ROW_NUMBER() OVER (
PARTITION BY OCCUPATION ORDER BY NAME) AS r,
CASE WHEN Occupation = 'Doctor' THEN Name END AS Doctor,
CASE WHEN Occupation = 'Professor' THEN Name END AS Professor,
CASE WHEN Occupation = 'Singer' THEN Name END AS Singer,
CASE WHEN Occupation = 'Actor' THEN Name END AS Actor
FROM OCCUPATIONS
) t
GROUP BY t.r
[MySQL Solution 3]
SELECT
GROUP_CONCAT(if(Occupation = 'Doctor', Name, NULL)) AS 'Doctor',
GROUP_CONCAT(if(Occupation = 'Professor', Name, NULL)) AS 'Professor',
GROUP_CONCAT(if(Occupation = 'Singer', Name, NULL)) AS 'Singer',
GROUP_CONCAT(if(Occupation = 'Actor', Name, NULL)) AS 'Actor'
FROM
(
SELECT a.*,
(CASE @vOccup WHEN a.OCCUPATION THEN @rownum:=@rownum+1 ELSE @rownum:=1 END) rnum, -- CASE WHEN @vOccup = a.OCCUPATION THEN ... 도 가능
(@vOccup:=a.OCCUPATION) vOccup
FROM OCCUPATIONS a,(SELECT @vOccup:='', @rownum:=0 FROM DUAL) b
ORDER BY OCCUPATION, NAME ) c
GROUP BY c.rnum
참고: https://mia-dahae.tistory.com/82
SELECT @vOccup:='', @rownum:=0 FROM DUAL;
문제 링크:
'SQL' 카테고리의 다른 글
[HackerRank] Advanced Select > New Companies (0) | 2022.12.28 |
---|---|
[HackerRank] Advanced Select > Binary Tree Nodes (0) | 2022.12.28 |
[HackerRank] Basic Select > Weather Observation Station 11 (0) | 2022.12.27 |
[HackerRank] Basic Select > Weather Observation Station 6, 7 (0) | 2022.12.26 |
[SQL][DDL] CREATE (0) | 2022.03.14 |