-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOccupations.sql
More file actions
21 lines (19 loc) · 974 Bytes
/
Occupations.sql
File metadata and controls
21 lines (19 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 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. */
--
-- Author: Pavith Bambaravanage
-- URL: https://github.com/Pavith19
--
SET @r1=0, @r2=0, @r3 =0, @r4=0;
SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor) FROM
(SELECT CASE Occupation WHEN 'Doctor' THEN @r1:=@r1+1
WHEN 'Professor' THEN @r2:=@r2+1
WHEN 'Singer' THEN @r3:=@r3+1
WHEN 'Actor' THEN @r4:=@r4+1 END
AS RowLine,
CASE WHEN Occupation = 'Doctor' THEN Name END AS Doctor,
CASE WHEN Occupation = 'Professor' THEN Name END AS Professor,
CASE WHEN Occupation = 'Singer' THEN Name END AS Singer,
CASE WHEN Occupation = 'Actor' THEN Name END AS Actor
FROM OCCUPATIONS ORDER BY Name) AS TEMP
GROUP BY Rowline;