-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.AS03-Occupations.sql
108 lines (80 loc) · 3.41 KB
/
05.AS03-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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
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:
+------------+--------+
| COLUMNS | 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.
*/
use hackerrank;
GO
raiserror('Now at the create procedure 2.3 Occupations....',0,1)
GO
CREATE or ALTER PROCEDURE advancedselect.proc_03occupations AS
Select [Doctor],[Professor],[Singer],[Actor]
From (
Select Occupation, Name, Row_Number() Over(Partition By Occupation Order by Name)SNo
From advancedselect.OCCUPATIONS
)tt
Pivot (Max(Name) For Occupation in ([Doctor],[Professor],[Singer],[Actor]))a
GO
CREATE or ALTER PROCEDURE AdvancedSelectTestClass.test_03occupations
AS
BEGIN
IF OBJECT_ID('actual') IS NOT NULL DROP TABLE actual;
IF OBJECT_ID('expected') IS NOT NULL DROP TABLE expected;
CREATE TABLE actual (
DOCTOR varchar(10) NULL,
PROFESSOR varchar(10) NULL,
SINGER varchar(10) NULL,
ACTOR varchar(10) NULL,
);
INSERT INTO actual (DOCTOR, PROFESSOR, SINGER, ACTOR) exec advancedselect.proc_03occupations
CREATE TABLE expected (
DOCTOR varchar(10) NULL,
PROFESSOR varchar(10) NULL,
SINGER varchar(10) NULL,
ACTOR varchar(10) NULL,
);
INSERT INTO expected (DOCTOR, PROFESSOR, SINGER, ACTOR) SELECT 'Aamina','Ashley','Christeen','Eve'
INSERT INTO expected (DOCTOR, PROFESSOR, SINGER, ACTOR) SELECT 'Julia','Belvet','Jane','Jennifer'
INSERT INTO expected (DOCTOR, PROFESSOR, SINGER, ACTOR) SELECT 'Priya','Britney','Jenny','Ketty'
INSERT INTO expected (DOCTOR, PROFESSOR, SINGER, ACTOR) SELECT NULL,'Maria','Kristeen','Samantha'
INSERT INTO expected (DOCTOR, PROFESSOR, SINGER, ACTOR) SELECT NULL,'Meera',NULL,NULL
INSERT INTO expected (DOCTOR, PROFESSOR, SINGER, ACTOR) SELECT NULL,'Naomi',NULL,NULL
INSERT INTO expected (DOCTOR, PROFESSOR, SINGER, ACTOR) SELECT NULL,'Priyanka',NULL,NULL
EXEC tSQLt.AssertEqualsTable 'expected', 'actual';
END
GO
--exec tSQLt.Run 'AdvancedSelectTestClass.[test_03occupations]';