+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
+-------------+---------+
This table may contain duplicate rows.
Each row of this table indicates the name of a student and the continent they came from.
A school has students from Asia, Europe, and America.
Write a solution to pivot the continent column in the Student table so that each name is sorted alphabetically and displayed underneath its corresponding continent. The output headers should be America, Asia, and Europe, respectively.
The test cases are generated so that the student number from America is not less than either Asia or Europe.
Input:
Student table:+--------+-----------+| name | continent |+--------+-----------+| Jane | America || Pascal | Europe || Xi | Asia || Jack | America |+--------+-----------+Output:
+---------+------+--------+| America | Asia | Europe |+---------+------+--------+| Jack | Xi | Pascal || Jane |null|null|+---------+------+--------+**Follow up:** If it is unknown which continent has the most students, could
you write a solution to generate the student report?
We want to pivot the continent column so that each continent is a column and each row contains the student names (sorted alphabetically) for that continent. We can use ROW_NUMBER() to assign a row index per continent, then join the results for each continent on the row index.
WITH america AS (
SELECT name, ROW_NUMBER() OVER (ORDERBY name) rn FROM Student WHERE continent ='America' ),
asia AS (
SELECT name, ROW_NUMBER() OVER (ORDERBY name) rn FROM Student WHERE continent ='Asia' ),
europe AS (
SELECT name, ROW_NUMBER() OVER (ORDERBY name) rn FROM Student WHERE continent ='Europe' )
SELECT a.name AS America, b.name AS Asia, c.name AS Europe
FROM america a
LEFTJOIN asia b ON a.rn = b.rn
LEFTJOIN europe cON a.rn =c.rn
ORDERBY a.name;
1
// Use the SQL query above in your JDBC/ORM code. No special Java logic needed.
1
2
3
4
5
6
7
8
9
# Assuming student is a pandas DataFrameamerica = student[student['continent'] =='America'].sort_values('name').reset_index(drop=True)
asia = student[student['continent'] =='Asia'].sort_values('name').reset_index(drop=True)
europe = student[student['continent'] =='Europe'].sort_values('name').reset_index(drop=True)
maxlen = max(len(america), len(asia), len(europe))
america = america['name'].reindex(range(maxlen))
asia = asia['name'].reindex(range(maxlen))
europe = europe['name'].reindex(range(maxlen))
result = pd.DataFrame({'America': america, 'Asia': asia, 'Europe': europe})