一道面试题引发的数据库行列转换实践
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一道面试题引发的数据库行列转换实践相关的知识,希望对你有一定的参考价值。
问题场景
最近有个朋友去面试,问了我一道面试题。题目如下,在形如下面的数据库表score中,找出每门成绩(grade)都大于等于80分的学生姓名。
----------------------------------------
name
| course | grade
----------------------------------------
zhangsan | Java | 70
----------------------------------------
zhangsan | C++ | 80
----------------------------------------
lisi | Java | 90
----------------------------------------
lisi | C++ | 60
----------------------------------------
wangwu | Java | 85
----------------------------------------
wangwu | C++ | 95
----------------------------------------
----------------------------------------
name
----------------------------------------
wangwu
----------------------------------------
select s.`name` as name,
(select grade from score where name = s.`name` and course = ‘Java‘)as ‘Java‘,
(select grade from score where name = s.`name` and course = ‘C++‘)as ‘C++‘
from score s
运行结果截图:
select distinct s.`name` as name,#此处加distinct来过滤相同的行
(select grade from score where name = s.`name` and course = ‘Java‘)as ‘Java‘,
(select grade from score where name = s.`name` and course = ‘C++‘)as ‘C++‘
from score s
运行结果截图:
3、最后通过构造一个子查询,即是把上面2的查询结果作为一个表来查询,进行 where 行级过滤就可以了,实现SQL语句如下:
select *
from
(
select distinct s.`name` as name,#此处加distinct来过滤相同的行
(select grade from score where name = s.`name` and course = ‘Java‘)as G1,
(select grade from score where name = s.`name` and course = ‘C++‘)as G2
from score s
) score
where G1>=80 and G2>=80
运行结果截图:
问题:这里有一个问题指出下,如果写成如下的sql语句,把Java和C++作为列名的话(还有C#),查询结果为NULL,这个问题后续会详解,请见运行结果截图:
select *
from
(
select distinct s.`name` as name,#此处加distinct来过滤相同的行
(select grade from score where name = s.`name` and course = ‘Java‘)as ‘Java‘,
(select grade from score where name = s.`name` and course = ‘C++‘)as ‘C++‘
from score s
) score
where ‘Java‘>=80 and ‘C++‘>=80
运行结果截图:
方案二
1、通过group和聚合函数sum的结合使用,通过group by name分组,利用sum假模假样计算出每门课程的成绩,sum的时候利用case判断课程类别,就得到了以行显示的每个学生的每门课程成绩,这一步最关键,实现SQL语句如下:
select name, sum(case when course=‘Java‘ then grade end) as ‘Java‘, sum(case when course=‘C++‘ then grade end) as ‘C++‘ from score group by name
运行结果截图:
2、再通过构造一个子查询,即是把上面1的查询结果作为一个表来查询,进行 where 行级过滤就可以了,实现SQL语句如下:
select * from ( select name, sum(case when course=‘Java‘ then grade end) as G1, sum(case when course=‘C++‘ then grade end) as G2 from score group by name ) score where G1>=80 and G2>=80
运行结果截图:
方案三
1、先找出有任意课程<80分的学生,实现SQL语句如下:
select name from score where grade<80
运行结果截图:
2、distinct出所有学生列表(不重复),实现SQL语句如下:
select distinct name from score
运行结果截图:
3、通过构造子查询从查询2的结果排除出去查询1的结果,这一步骤有的数据库是有集合函数,比如SQL Server的Except,这儿我们用not exists进行行级过滤,实现SQL语句如下:
select * from ( select distinct name from score ) score1 where not exists ( select * from ( select distinct name from score where grade<80 ) score2 where score1.name=score2.name )
运行结果截图:
总结:
以上是关于一道面试题引发的数据库行列转换实践的主要内容,如果未能解决你的问题,请参考以下文章