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.
set @D = 0, @A = 0, @S = 0, @P = 0;
/* 각 직업별 이름 index 설정*/
/* min -> 알파벳 순서 최소값 추출*/
select min(Doctor), min(Professor), min(Singer), min(Actor)
from (select
case when Occupation = 'Doctor' then Name end as Doctor,
case when Occupation = 'Actor' then Name end as Actor,
case when Occupation = 'Singer' then Name end as Singer,
case when Occupation = 'Professor' then Name end as Professor,
case
when Occupation = 'Doctor' then (@D:=@D+1)
when Occupation = 'Actor' then (@A:=@A+1)
when Occupation = 'Singer' then (@S:=@S+1)
when Occupation = 'Professor' then (@P:=@P+1)
end as temp_number
from OCCUPATIONS
order by Name) as temp
group by temp_number