随笔 - 384  文章 - 0  评论 - 0  阅读 - 13万

力扣618(MySQL)-学生地理信息报告(困难)

题目:

 一所美国大学有来自亚洲、欧洲和美洲的学生,他们的地理信息存放在如下 student 表中

该表没有主键。它可能包含重复的行。
该表的每一行表示学生的名字和他们来自的大陆。

一所学校有来自亚洲、欧洲和美洲的学生。

示例:

student:

 写一个查询语句实现对大洲(continent)列的 透视表 操作,使得每个学生按照姓名的字母顺序依次排列在对应的大洲下面。输出的标题应依次为美洲(America)、亚洲(Asia)和欧洲(Europe)。数据保证来自美洲的学生不少于来自亚洲或者欧洲的学生。
输出:

 解题思路:

行转列---需要使用CASE...WHEN...

①行转列,会出现很多null值

1 select 
2    case when continent ='America' then name else null end as America,
3    case when continent ='Asia' then name else null end as Asia,
4    case when continent ='Europe' then name else null end as Europe
5 from student_618;

 ②使用max()或min()保留出姓名的最大值或最小值,但是每一列只能保存下一个值

1 select 
2    min(case when continent ='America' then name else null end) as America,
3    min(case when continent ='Asia' then name else null end) as Asia,
4    min(case when continent ='Europe' then name else null end) as Europe
5 from student_618;

③通过将studnet 表以continent分组name值进行排序;

1 select name ,continent, row_number() over(partition by continent order by name) as rnk
2 from student_618

最后再按序号进行分组聚合,就能得到每个序号的最小值了,这样所有值都能保留下来。

复制代码
1 select 
2    min(case when continent ='America' then name else null end) as America,
3      min(case when continent ='Asia' then name else null end) as Asia,
4      min(case when continent ='Europe' then name else null end) as Europe
5 from (
6 select name ,continent, row_number() over(partition by continent order by name) as rnk
7 from student_618
8 ) as temp
9 group by rnk;
复制代码

 小知识:

①rank() over():相同的序号一样,下一个不同数跳跃序数,例如:1,2,2,4

dense_rank() over():相同的序号一样,下一个不同数连续序数,例如:1,2,2,3

row_number() over():无论相同与否,按顺序排序,例如:1,2,3,4

②case...when...语法:

复制代码
-- 语法一
case 要判断的字段或表达式
when 常量1 then 要显示的值1或语句1
when 常量2 then 要显示的值2或语句2
...
else 要显示的值n或语句n
end
-- 语法二
case
when 条件1 then 要显示的值1或语句1
when 条件2 then 要显示的值2或语句2
...
else 要显示的值n或语句n
end
复制代码
posted on   我不想一直当菜鸟  阅读(230)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示