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 DoctorProfessorSinger, and Actor, respectively.

Note: Print NULL when there are no more names corresponding to an occupation.

 

 

Pivot Syntax:

SELECT first_column AS <first_column_alias>,
[pivot_value1], [pivot_value2], ... [pivot_value_n]
FROM 
(<source_table>) AS <source_table_alias>
PIVOT 
(
 aggregate_function(<aggregate_column>)
 FOR <pivot_column>
 IN ([pivot_value1], [pivot_value2], ... [pivot_value_n])
) AS <pivot_table_alias>;

 

Table:

 

 

SELECT Doctor, Professor, Singer, Actor
from
(
  SELECT Name, Occupation,
    row_number() over(partition by Occupation order by Name) rn
  from Occupations
) as st
pivot
(
  max(Name)
  FOR Occupation in (Doctor, Professor, Singer, Actor)
) as pivottable