当前位置:
首页
文章
前端
详情

Hive实战之学生课程成绩

基表:

use myhive;CREATE TABLE `course` (
  `id` int,
  `sid` int ,
  `course` string,
  `score` int 
) ;

INSERT INTO `course` VALUES (1, 1, 'yuwen', 43);INSERT INTO `course` VALUES (2, 1, 'shuxue', 55);INSERT INTO `course` VALUES (3, 2, 'yuwen', 77);INSERT INTO `course` VALUES (4, 2, 'shuxue', 88);INSERT INTO `course` VALUES (5, 3, 'yuwen', 98);INSERT INTO `course` VALUES (6, 3, 'shuxue', 65);

Hive实战之学生课程成绩

 需求:所有数学课程成绩 大于 语文课程成绩的学生的学号。

实现需求步骤;

  1,使用case...when...将不同的课程名称转换成不同的列。

create view tmp as select sid ,
case course when "shuxue" then score else 0 end as shuxue,
case course when "yuwen" then score else 0 end as yuwen 
from  course;

  2,以sid分组并获取个成绩的最大值

create view tmp_view 
as select sid as sid ,max(shuxue) as shuxue ,max(yuwen) as yuwen
from tmp group by sid;

  3,比较结果

select  * from tmp_view where shuxue>yuwen;

免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。