You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
--A U.S graduate school has students from Asia, Europe and America. The students' location information are stored in table student as below.
4
+
--
5
+
--| name | continent |
6
+
--|--------|-----------|
7
+
--| Jack | America |
8
+
--| Pascal | Europe |
9
+
--| Xi | Asia |
10
+
--| Jane | America |
11
+
--Pivot the continent column in this table so that each name is sorted alphabetically and displayed underneath its corresponding continent. The output headers should be America, Asia and Europe respectively. It is guaranteed that the student number from America is no less than either Asia or Europe.
12
+
--For the sample input, the output is:
13
+
--| America | Asia | Europe |
14
+
--|---------|------|--------|
15
+
--| Jack | Xi | Pascal |
16
+
--| Jane | | |
17
+
--Follow-up: If it is unknown which continent has the most students, can you write a query to generate the student report?
18
+
19
+
--reference: https://discuss.leetcode.com/topic/92752/accept-solution and https://leetcode.com/articles/students-report-by-geography/#approach-using-session-variables-and-join-accepted
20
+
21
+
set @a =0;
22
+
set @b =0;
23
+
set @c =0;
24
+
25
+
SelectAmerica.nameas America, Asia.nameas Asia, Europe.nameas Europe from
26
+
(select name, @a := @a +1as id from student where continent ='America'order by name) as America
27
+
Left Join
28
+
(select name, @b := @b +1as id from student where continent ='Asia'order by name) as Asia onAmerica.id=Asia.id
29
+
Left Join
30
+
(select name, @c := @c +1as id from student where continent ='Europe'order by name) as Europe onAmerica.id=Europe.id
0 commit comments