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.
The OCCUPATIONS table is described as follows:
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
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.
To solve this problem, we can use user-defined variables to help create a new table. Take the sample input as example, the table we want to create looks like below:
The RowLine represents the line where the name should be put. In addition, because we want to sort names alphabetically for each occupation, the first step of creating the table above is to sort OCCUPATIONS table by name. Let’s call the table t. Once we have got the table t, we can use “SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor) FROM t GROUP BY RowLine” to get the result.
To get table t, user-defined variables and CASE operator can help. We create four variables to record the line number RowLine, one for each occupation. We use CASE to add variables according to occupation.
This solution is for MS SQL Server
SELECT MIN(DOCTOR),MIN(PROFESSOR), MIN(SINGER), MIN(ACTOR) FROM ( -- AS Y SELECT RN1, DOCTOR, PROFESSOR, SINGER, ACTOR FROM ( -- AS X SELECT --MAIN CASE OCCUPATION WHEN 'Doctor' THEN NAME END DOCTOR, CASE OCCUPATION WHEN 'Professor' THEN NAME END PROFESSOR, CASE OCCUPATION WHEN 'Singer' THEN NAME END SINGER, CASE OCCUPATION WHEN 'Actor' THEN NAME END ACTOR, OCCUPATION, NAME, RN1 = ROW_NUMBER() OVER (PARTITION BY OCCUPATION ORDER BY NAME ASC) FROM OCCUPATIONS ) X ) Y GROUP BY RN1