-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOccupations.sql
64 lines (44 loc) · 1.71 KB
/
Occupations.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Occupations
-----------------------------------
Link: https://www.hackerrank.com/contests/wissen-coding-challenge-2021/challenges/occupations
-----------------------------------
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
Ashley|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.
*/
(Oracle Solution)
select doctor, professor, singer, actor
from
(select name, occupation, row_number() over (partition by occupation order by name asc) rn from occupations)
pivot (
min(name) for occupation in ('Doctor' as doctor,'Professor' as professor,'Singer' as singer,'Actor' as actor)
)
order by rn;