-
Notifications
You must be signed in to change notification settings - Fork 183
/
03 - Occupations.sql
46 lines (43 loc) · 1.34 KB
/
03 - 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
-- ========================
-- Information
-- ========================
-- Direct Link: https://www.hackerrank.com/challenges/occupations/problem
-- Difficulty: Medium
-- Max Score: 30
-- DBMS: mySQL
-- ========================
-- Solution
-- ========================
SET @d=0, @a=0, @p=0, @s=0;
SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor)
FROM
(SELECT
CASE Occupation
WHEN 'Doctor'
THEN @d:=@d+1
WHEN 'Professor'
THEN @a:=@a+1
WHEN 'Singer'
THEN @p:=@p+1
WHEN 'Actor'
THEN @s:=@s+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 t
GROUP BY RowLine;