Skip to content

Commit d429261

Browse files
authored
Create occupations.sql
1 parent bc8cda1 commit d429261

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/*
3+
Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation.
4+
The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
5+
6+
Note: Print NULL when there are no more names corresponding to an occupation.
7+
*/
8+
9+
set @r1=0, @r2=0, @r3=0, @r4=0;
10+
select min(Doctor), min(Professor), min(Singer), min(Actor)
11+
from(
12+
select case when Occupation='Doctor' then (@r1:=@r1+1)
13+
when Occupation='Professor' then (@r2:=@r2+1)
14+
when Occupation='Singer' then (@r3:=@r3+1)
15+
when Occupation='Actor' then (@r4:=@r4+1) end as RowNumber,
16+
case when Occupation='Doctor' then Name end as Doctor,
17+
case when Occupation='Professor' then Name end as Professor,
18+
case when Occupation='Singer' then Name end as Singer,
19+
case when Occupation='Actor' then Name end as Actor
20+
from OCCUPATIONS
21+
order by Name
22+
) temp
23+
group by RowNumber;

0 commit comments

Comments
 (0)