Problem
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:
Column | Type |
---|---|
Name | String |
Occupation | String |
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
Sample Input
Name | Occupation |
---|---|
Samantha | Doctor |
Julia | Actor |
Maria | Actor |
Meera | Singer |
Ashely | Professor |
Ketty | Professor |
Christeen | Professor |
Jane | Actor |
Jenny | Doctor |
Priya | Singer |
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.
Analysis
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:
RowLine | Doctor | Professor | Singer | Actor |
---|---|---|---|---|
1 | NULL | Ashely | NULL | NULL |
2 | NULL | Christeen | NULL | NULL |
1 | NULL | NULL | NULL | Jane |
1 | Jenny | NULL | NULL | NULL |
2 | NULL | NULL | NULL | Julia |
3 | NULL | Ketty | NULL | NULL |
3 | NULL | NULL | NULL | Maria |
1 | NULL | NULL | Meera | NULL |
2 | NULL | NULL | Priya | NULL |
2 | Samantha | NULL | NULL | NULL |
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.
Solution
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