HackerRank Occupations SQL Server solution

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 DoctorProfessorSinger, 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:

ColumnType
NameString
OccupationString

Occupation will only contain one of the following values: DoctorProfessorSinger or Actor.

Sample Input

NameOccupation
SamanthaDoctor
JuliaActor
MariaActor
MeeraSinger
AshelyProfessor
KettyProfessor
ChristeenProfessor
JaneActor
JennyDoctor
PriyaSinger

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:

RowLineDoctorProfessorSingerActor
1NULLAshelyNULLNULL
2NULLChristeenNULLNULL
1NULLNULLNULLJane
1JennyNULLNULLNULL
2NULLNULLNULLJulia
3NULLKettyNULLNULL
3NULLNULLNULLMaria
1NULLNULLMeeraNULL
2NULLNULLPriyaNULL
2SamanthaNULLNULLNULL

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

Yorum bırakın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir