Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9646336

Browse files
add 618
1 parent 92e119a commit 9646336

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ Your ideas/fixes/algorithms are more than welcome!
610610
|627|[Swap Salary](https://leetcode.com/problems/swap-salary/)|[Solution](../master/database/_627.sql) | | | Easy |
611611
|620|[Not Boring Movies](https://leetcode.com/problems/not-boring-movies/)|[Solution](../master/database/_620.sql) | | | Easy |
612612
|619|[Biggest Single Number](https://leetcode.com/problems/biggest-single-number/)|[Solution](../master/database/_619.sql) | | | Easy |
613+
|618|[Students Report By Geography](https://leetcode.com/problems/students-report-by-geography/)|[Solution](../master/database/_618.sql) | | | Hard | Session Variables
613614
|614|[Second Degree Follower](https://leetcode.com/problems/second-degree-follower/)|[Solution](../master/database/_614.sql) | | | Medium | Inner Join
614615
|613|[Shortest Distance in a Line](https://leetcode.com/problems/shortest-distance-in-a-line/)|[Solution](../master/database/_613.sql) | || Easy|
615616
|612|[Shortest Distance in a Plane](https://leetcode.com/problems/shortest-distance-in-a-plane/)|[Solution](../master/database/_612.sql) | || Medium|

database/_618.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--618. Students Report By Geography
2+
--
3+
--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+
Select America.name as America, Asia.name as Asia, Europe.name as Europe from
26+
(select name, @a := @a + 1 as id from student where continent = 'America' order by name) as America
27+
Left Join
28+
(select name, @b := @b + 1 as id from student where continent = 'Asia' order by name) as Asia on America.id = Asia.id
29+
Left Join
30+
(select name, @c := @c + 1 as id from student where continent = 'Europe' order by name) as Europe on America.id = Europe.id

0 commit comments

Comments
 (0)