Hive _练习,更新中
生活随笔
收集整理的這篇文章主要介紹了
Hive _练习,更新中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
#學生表 create table student( s_id string, s_name string, s_birth string, s_sex string ) row format delimited fields terminated by ' ';#sid,sname,sbirth,ssex #學生信息 01 George 1990-01-01 男 02 honey 1990-12-21 男 03 kk 1990-05-20 男 04 ll 1990-08-06 男 05 Georgedage 1991-12-01 女 06 lm 1992-03-01 女 07 lucy 1989-07-01 女 08 lili 1990-01-20 女#課程表 create table course( c_id string, c_name string, t_id string ) row format delimited fields terminated by ' ';#cid,cname,tid #課程信息 01 c 02 02 java 01 03 python 03#教師表 create table teacher( t_id string, t_name string ) row format delimited fields terminated by ' ';#tid,tname #教室信息 01 wu 02 li 03 yuan#成績表 create table score( s_id string, c_id string, s_score int ) row format delimited fields terminated by ' ';#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#2、導入數據 load data local inpath "/root/student" into table student; load data local inpath "/root/course" into table course; load data local inpath "/root/teacher" into table teacher; load data local inpath "/root/score" into table score;select * from course,score;select student.*,score.s_score from student,score where student.s_id=score.s_id;– 1、查詢"01"課程比"02"課程成績高的學生的信息及課程分數: *第一種寫法 select student.*,a.s_score as 01_score,b.s_score as 02_score from studentjoin score a on student.s_id=a.s_id and a.c_id='01'left join score b on student.s_id=b.s_id and b.c_id='02' where a.s_score>b.s_score; *第二種寫法 select student.*,sc1.s_score,sc2.s_score from student,score sc1,score sc2 where student.s_id=sc1.s_id and student.s_id=sc2.s_id and sc1.c_id='01' and sc2.c_id='02' and sc1.s_score>sc2.s_score;~結果: 02 honey 1990-12-21 男 70 60 04 ll 1990-08-06 男 50 30– 2、查詢"01"課程比"02"課程成績低的學生的信息及課程分數: *第一種寫法 select student.*,sc1.s_score,sc2.s_score from student,score sc1,score sc2 where student.s_id=sc1.s_id and student.s_id=sc2.s_id and sc1.c_id='01' and sc2.c_id='02' and sc1.s_score<sc2.s_score; *第二種寫法 select student.*,sc1.s_score,sc2.s_score from student join score sc1 on student.s_id=sc1.s_id and sc1.c_id='01' join score sc2 on student.s_id=sc2.s_id and sc2.c_id='02' where sc1.s_score<sc2.s_score;~結果: 01 George 1990-01-01 男 80 90 05 Georgedage 1991-12-01 女 76 87select score.s_id,avg(score.s_score) from score group by s_id having avg(score.s_score)>60;select student.s_id,student.s_name,round(avg (score.s_score),1) as 平均成績 from student join score on student.s_id = score.s_id group by student.s_id,student.s_name having avg (score.s_score) >= 60;– 3、查詢平均成績大于等于60分的同學的學生編號和學生姓名和平均成績: *第一種寫法 select student.s_id,student.s_name,sc.avg2 from student,(select score.s_id id,avg(score.s_score) avg2 from score group by s_id having avg(score.s_score)>60) as sc where student.s_id=sc.id; *第二種寫法【既然不讓我用where,那我就用on】 select student.s_id,student.s_name,round(avg(score.s_score),1) as avg2 from student join score on student.s_id=score.s_id group by student.s_id,student.s_name having avg(score.s_score)>60;~結果: 01 George 89.66666666666667 02 honey 70.0 03 kk 80.0 05 Georgedage 81.5 07 lucy 93.5– 4、查詢平均成績小于60分的同學的學生編號和學生姓名和平均成績: – (包括有成績的和無成績的) *第一種寫法 select student.s_id,student.s_name,round(avg(score.s_score),1) as avg2 from student left join score on student.s_id= score.s_id group by student.s_id,student.s_name having avg(score.s_score)<60; ~結果: 04 ll 33.3 06 lm 32.5– 5、查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績: *第一種寫法 select student.s_id,student.s_name,count(score.c_id),sum(score.s_score) from student left join score on student.s_id=score.s_id group by student.s_id,student.s_name; ~結果: 01 George 3 269 02 honey 3 210 03 kk 3 240 04 ll 3 100 05 Georgedage 2 163 06 lm 2 65 07 lucy 2 187 08 lili 0 NULL– 6、查詢"李"姓老師的數量: *第一種寫法 select count(t_id) from teacher t where t.t_name like '李%'; ~結果: 1#編碼問題,所以改變老師名字。– 7、查詢學過"吳老大"老師授課的同學的信息: *第一種寫法 select s.* from student s where s.s_id in (select s_id from score sc,course c,teacher t where sc.c_id = c.c_id and c.t_id = t.t_id and t.t_name='wu'); ~結果:– 8、查詢沒學過"吳老大"老師授課的同學的信息: select s.* from student s where s.s_id not in (select s_id from score sc,course c,teacher t where sc.c_id = c.c_id and c.t_id = t.t_id and t.t_name = 'wu'); ~結果: 06 lm 1992-03-01 女 08 lili 1990-01-20 女– 9、查詢學過編號為"01"并且也學過編號為"02"的課程的同學的信息: *第一種寫法 select s.* from student s,(select s_id from score where score.c_id=01) sc1,(select s_id from score where score.c_id=02) sc2 where sc1.s_id=sc2.s_id and sc1.s_id=s.s_id; ~結果: 01 George 1990-01-01 男 02 honey 1990-12-21 男 03 kk 1990-05-20 男 04 ll 1990-08-06 男 05 Georgedage 1991-12-01 女?
總結
以上是生活随笔為你收集整理的Hive _练习,更新中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hive函数简介
- 下一篇: no.2_用绳子计时15分钟