LeetCode(数据库)- 学生地理信息报告
Posted Lux_Sun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 学生地理信息报告相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:原题此解决方案是需要一个前提——美洲学生最多,否则第 7 个用例通不过,如图所示,但题目没说,有点坑;进阶题是即使没有说明哪个洲学生最多也可以解(列转行思想)。
AC 代码
-- 原题
WITH t1 AS(SELECT *, ROW_NUMBER() OVER(ORDER BY name) rnk FROM student WHERE continent = 'America'),
t2 AS(SELECT *, ROW_NUMBER() OVER(ORDER BY name) rnk FROM student WHERE continent = 'Asia'),
t3 AS(SELECT *, ROW_NUMBER() OVER(ORDER BY name) rnk FROM student WHERE continent = 'Europe')
SELECT t1.name America, t2.name Asia, t3.name Europe
FROM t1 LEFT JOIN t2 ON t1.rnk = t2.rnk LEFT JOIN t3 ON t1.rnk = t3.rnk
-- 进阶
select
max(case when continent='America' then name else null end) as America
,max(case when continent='Asia' then name else null end) as Asia
,max(case when continent='Europe' then name else null end) as Europe
from (SELECT *, row_number() over(partition by continent order by name) as rn from student) t
group by rn
以上是关于LeetCode(数据库)- 学生地理信息报告的主要内容,如果未能解决你的问题,请参考以下文章