Hive 基础测试(二)

准备数据

  • 学生表student
         学号sid,姓名sname,性别ssex,年龄sage,系 department
         95001,李勇,男,20,CS
         95002,刘晨,女,19,IS
         95003,王敏,女,22,MA
         95004,张立,男,19,IS
         95005,刘刚,男,18,MA
         95006,赵丽,女,20,CS
  • 成绩表score
         学生编号sid,课程编号cid,成绩sscore 
         95001,1,81
         95001,2,85
         95001,3,88
         95001,4,70
         95002,1,90
         95002,2,80
         95002,3,90
         95002,4,80
         95003,1,70
         95003,2,78
         95003,3,65
         95003,4,65
         95004,1,70
         95004,2,90
         95004,3,85
         95004,4,90
         95005,1,70
         95005,2,90
         95005,3,70
         95005,4,90
         95006,1,70
         95006,2,90
         95006,3,70
         95006,4,90
  • 课程表course
         课程编号cid,课程名cname
         1,数据库
         2,数学
         3,信息系统
         4,操作系统

需求1:创建三个外部表,并分别给外部表加载数据.

create external  table student(
 sid string,
 sname string,
 ssex string,
 sage int)
row format delimited fields terminated by ',';
load data local inpath '/root/test/student.txt' into table student;


create external  table score(
sid string,
cid string,
 sscore int)
row format delimited fields terminated by ',';
load data local inpath '/root/test/score.txt' into table score;


create  external table course(
cid string,
cname string)
row format delimited fields terminated by ',';
load data local inpath '/root/test/course.txt' into table course;

需求2:查询各课的学生人数。

select  b.cname, a.num
from 
(select cid ,count(*)  num from score group by cid) a, course b
where a.cid=b.cid;

需求3: 查询每个学生的总成绩,并对总成绩进行降序排序,要求显示字段:学生名字,学生学号,学生总成绩

select  st.sname ,st.sid  ,sum(sc.sscore)
from student st
    join score  sc
        on st.sid=sc.sid
group by st.sname, st.sid;

需求4:求总课程平均分最高的前三名学生

select sname,b.dr,b.avgScore
from(select a.sid,avgScore,dense_rank() over(order by avgScore desc) dr
from (select sid,avg(sscore) avgScore
from score group by sid) a) b,student 
where b.sid = student.sid and dr<4;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人间清醒vv子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值