0
点赞
收藏
分享

微信扫一扫

LeetCode(数据库)- 学生地理信息报告


题目链接:​​点击打开链接​​

题目大意:略。

解题思路:原题此解决方案是需要一个前提——美洲学生最多,否则第 7 个用例通不过,如图所示,但题目没说,有点坑;进阶题是即使没有说明哪个洲学生最多也可以解(列转行思想)。

LeetCode(数据库)- 学生地理信息报告_数据库


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


举报

相关推荐

0 条评论