Hive hql 练习
数据:
Student(Sid,Sname,Sage,Ssex)学生表
Sid:学号 Sname:学生姓名 Sbirth:学生生日 Ssex:学生性别
01 赵雷 1990-01-01 男
02 钱电 1990-12-21 男
03 孙风 1990-05-20 男
04 李云 1990-08-06 男
05 周梅 1991-12-01 女
06 吴兰 1992-03-01 女
07 郑竹 1989-07-01 女
08 王菊 1990-01-20 女
Course(Cid,Cname,T#)课程表
Cid:课程编号 Cname:课程名称 Tid:教师编号
01 语文 02
02 数学 01
03 英语 03
Teacher(Tid,Tname)教师表
Tid:教师编号 Tname:教师名字
01 张三
02 李四
03 王五
SC(Sid,Cid,score)成绩表
Sid:学号 Cid:课程编号 score:成绩
01 01 80
01 02 90
01 03 99
02 01 70
02 02 60
02 03 80
03 01 80
03 02 80
03 03 80
04 01 50
04 02 30
04 03 20
05 01 76
05 02 87
06 01 31
06 03 34
07 02 89
07 03 98
1 show databases; 2 use work8; 3 show tables;
//1:查询01课程比02课程成绩高的所有学生的学号
4 with 5 a as (select s.sid,score from student s left join sc on s.sid=sc.sid and cid=1), 6 b as (select s.sid,score from student s left join sc on s.sid=sc.sid and cid=2), 7 c as (select a.sid,a.score score1,b.score score2 from a left join b on a.sid=b.sid) 8 select sid from c where score1>score2;
//2:按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,
//--按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分 9 //--按如下形式显示:学生ID,语文成绩,数学成绩,英语成绩,有效课程数,有效平均分 10 with 11 a as (select s.sid,sc1.score,sc2.score,sc3.score from student s 12 left join sc sc1 on s.sid=sc1.sid and sc1.cid=1 13 left join sc sc2 on s.sid=sc2.sid and sc2.cid=2 14 left join sc sc3 on s.sid=sc3.sid and sc3.cid=3), 15 b as (select sid,count(sc.cid) yx,sum(score) ,round(sum(score)/count(sc.cid),2) av from sc group by sid ) 16 select a.*,b.yx,b.av from a right join b on a.sid=b.sid order by av desc;
// 17 select sid,count(sc.cid) yx,sum(score) ,round(sum(score)/count(sc.cid),2) av from sc group by sid order by av desc; 18 //3:查询各科成绩最高和最低的分:以如下的形式显示:课程ID,最高分,最低分 19 select cid,max(score),min(score) from sc group by cid; 20 //4:查询各科成绩前三名的记录(不考虑成绩并列情况) 21 select b.cname,a.score,a.rk from 22 (select sc.*,row_number() over (distribute by cid sort by score desc ) rk from sc ) a 23 join course b on a.cid=b.cid and a.rk<4; 24 //5:查询男生、女生人数 25 select ssex,count(1) from student group by ssex; 26 //6:求选了课程的学生人数 27 select count(0) from (select distinct sid from sc) a; 28 //7:查询每门课程成绩最好的前两名 29 select b.cname,c.sname,a.score,a.rk from 30 (select sid,cid,score,rank() over(distribute by cid sort by score desc) rk from sc) a 31 left join course b on a.cid=b.cid 32 left join student c on a.sid=c.sid where rk<3; 33 //8:查询平均成绩大于60分的同学的学号和平均成绩 34 select sid,avg(score) av from sc group by sid having av >60; 35 36 37 38 //1:查询和02号的同学学习的课程完全相同的其他同学学号和姓名 39 select b.sid,s.sname from 40 (select sum(cid) a from sc group by sid having sid=2) a, 41 (select sid,sum(cid) b from sc group by sid) b, 42 student s 43 where a.a=b.b and b.sid =s.sid and b.sid!=2; 44 //2:查询如下课程平均成绩和及格率的百分数(用”1行”显示): 语文(01),数学(02),英语(03) 45 select 46 max(case a.cid when 1 then concat(b.cname,a.av,':',a.pr) else 0 end) as yuwen, 47 max(case a.cid when 2 then concat(b.cname,a.av,":",a.pr) else 0 end)as shuxue , 48 max(case a.cid when 3 then concat(b.cname,a.av,":",a.pr) else 0 end)as yingyv 49 from 50 (select cid,round(avg(score),2) av,concat(round(sum(case when score>60 then 1 else 0 end)/count(score)*100,2),"%") pr from sc group by sc.cid )a join course b on a.cid=b.cid; 51 52 //3:统计下列各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ 小于60] 53 select b.cname,a.* from 54 (select cid,sum(case when score>85 then 1 else 0 end) as 100_85,sum(case when 85>=score and score>70 then 1 else 0 end) 85_70,sum(case when 70>score and score>60 then 1 else 0 end) 70_60 ,sum(case when score<60 then 1 else 0 end) 60_ from sc group by sc.cid) a 55 join course b on a.cid=b.cid; 56 //4:查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列 57 select sc.cid,b.cname,avg(score) av from sc left join course b on sc.cid=b.cid group by sc.cid,b.cname order by av asc , sc.cid desc; 58 //5:查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩 59 60 select s.sname,sc.score from sc left join course b on sc.cid=b.cid left join teacher c on c.tid=b.tid left join student s on sc.sid=s.sid where c.tname="张三" order by score desc limit 1; 61 //6:查询全部学生选修的课程的课程号和课程名 62 63 select sc.cid,b.cname from sc left join course b on b.cid=sc.cid group by sc.cid ,b.cname;