Students Report By Geography Problem


Description

LeetCode Problem 618.

A U.S graduate school has students from Asia, Europe and America. The students’ location information are stored in table student as below.

1
2
3
4
5
6
| name   | continent |
|--------|-----------|
| Jack   | America   |
| Pascal | Europe    |
| Xi     | Asia      |
| Jane   | America   |

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.

For the sample input, the output is:

1
2
3
4
| America | Asia | Europe |
|---------|------|--------|
| Jack    | Xi   | Pascal |
| Jane    |      |        |


MySQL Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
select America, Asia, Europe
from 
    (select name as America, row_number() over () as rn
    from student
    where continent = 'America'
    order by America) a
left join     
    (select name as Asia, row_number() over () as tn
    from student
    where continent = 'Asia'
    order by Asia)  b
on rn = tn    
left join
    (select name as Europe, row_number() over () as kn
    from student
    where continent = 'Europe'
    order by Europe)  c    
on rn = kn




Related Posts

Students Report By Geography Problem

LeetCode 618. A U.S graduate school has students from Asia,...

Average Salary: Departments VS Company Problem

LeetCode 615. Given two tables as below, write a query...

Second Degree Follower Problem

LeetCode 614. Please write a sql query to get the...

Shortest Distance In A Plane Problem

LeetCode 612. Write a query to find the shortest distance...

Triangle Judgement Problem

LeetCode 610. A pupil Tim gets homework to identify whether...

Shortest Distance Problem

LeetCode 613. Write a query to find the shortest distance...