From e9388fa4241397dd2fbbf2214bdd621df5f4a2f3 Mon Sep 17 00:00:00 2001 From: Openset Date: Tue, 29 Jan 2019 21:41:02 +0800 Subject: [PATCH 1/2] Add: Students Report By Geography --- .../students-report-by-geography/README.md | 22 +++++++++++++- .../students_report_by_geography.go | 1 - .../students_report_by_geography.sql | 29 +++++++++++++++++++ .../students_report_by_geography_test.go | 1 - 4 files changed, 50 insertions(+), 3 deletions(-) delete mode 100644 problems/students-report-by-geography/students_report_by_geography.go create mode 100644 problems/students-report-by-geography/students_report_by_geography.sql delete mode 100644 problems/students-report-by-geography/students_report_by_geography_test.go diff --git a/problems/students-report-by-geography/README.md b/problems/students-report-by-geography/README.md index cf4648fe3..9a5989a4a 100644 --- a/problems/students-report-by-geography/README.md +++ b/problems/students-report-by-geography/README.md @@ -1,2 +1,22 @@ -## 618. Students Report By Geography +## 618. Students Report By Geography (Hard) +A U.S graduate school has students from Asia, Europe and America. The students' location information are stored in table student as below.

+ +
| 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:

+
| America | Asia | Europe |
+|---------|------|--------|
+| Jack    | Xi   | Pascal |
+| Jane    |      |        |
+

+ +Follow-up: If it is unknown which continent has the most students, can you write a query to generate the student report?

diff --git a/problems/students-report-by-geography/students_report_by_geography.go b/problems/students-report-by-geography/students_report_by_geography.go deleted file mode 100644 index 7e0589efe..000000000 --- a/problems/students-report-by-geography/students_report_by_geography.go +++ /dev/null @@ -1 +0,0 @@ -package students_report_by_geography diff --git a/problems/students-report-by-geography/students_report_by_geography.sql b/problems/students-report-by-geography/students_report_by_geography.sql new file mode 100644 index 000000000..1c99109b1 --- /dev/null +++ b/problems/students-report-by-geography/students_report_by_geography.sql @@ -0,0 +1,29 @@ +# Write your MySQL query statement below + +SELECT + America, Asia, Europe +FROM + (SELECT @as:=0, @am:=0, @eu:=0) t, + (SELECT + @as:=@as + 1 AS asid, name AS Asia + FROM + student + WHERE + continent = 'Asia' + ORDER BY Asia) AS t1 + RIGHT JOIN + (SELECT + @am:=@am + 1 AS amid, name AS America + FROM + student + WHERE + continent = 'America' + ORDER BY America) AS t2 ON asid = amid + LEFT JOIN + (SELECT + @eu:=@eu + 1 AS euid, name AS Europe + FROM + student + WHERE + continent = 'Europe' + ORDER BY Europe) AS t3 ON amid = euid; diff --git a/problems/students-report-by-geography/students_report_by_geography_test.go b/problems/students-report-by-geography/students_report_by_geography_test.go deleted file mode 100644 index 7e0589efe..000000000 --- a/problems/students-report-by-geography/students_report_by_geography_test.go +++ /dev/null @@ -1 +0,0 @@ -package students_report_by_geography From 6d7dbb216064a515b33f8cd20f391f1c6f2a7e35 Mon Sep 17 00:00:00 2001 From: Openset Date: Tue, 29 Jan 2019 21:43:53 +0800 Subject: [PATCH 2/2] Update: README --- problems/biggest-single-number/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/biggest-single-number/README.md b/problems/biggest-single-number/README.md index c4c5fb025..ffc1ab6da 100644 --- a/problems/biggest-single-number/README.md +++ b/problems/biggest-single-number/README.md @@ -1,4 +1,4 @@ -## 619. Biggest Single Number +## 619. Biggest Single Number (Easy)

Table number contains many numbers in column num including duplicated ones.