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.
#나의 해답
SELECT Doctor, Professor, Singer, Actor
FROM (SELECT OCCUPATION, NAME, ROW_NUMBER() OVER(PARTITION BY OCCUPATION ORDER BY NAME)
FROM OCCUPATIONS
)
PIVOT (
MIN(NAME) FOR OCCUPATION IN ('Doctor' AS Doctor
, 'Professor' AS Professor
, 'Singer' AS Singer
, 'Actor' AS Actor)
)
ORDER BY 1, 2, 3, 4;
'빅데이터 관련 자료 > [SQL] Basic' 카테고리의 다른 글
Binary Tree Nodes (0) | 2021.09.06 |
---|---|
데이터베이스 기초 (MYSQL) (0) | 2021.09.02 |
The PADS (0) | 2021.08.14 |
Type of Triangle (0) | 2021.08.13 |
Employee Salaries (0) | 2021.08.12 |