cd /export/servers/hivedatas hdfs dfs -mkdir -p /hivedatas hdfs dfs -put /export/servers/hivedatas/teacher.csv /hivedatas/ load data inpath '/hivedatas/teacher.csv' into table teacher;
创建普通表,并向表中加载数据
createtable course (c_id string,c_name string,t_id string) row format delimited fields terminated by'\t'; load data local inpath '/export/servers/hivedatas/course.csv'intotable course; createtable score (s_id string,c_id string,s_score int) row format delimited fields terminated by'\t'; load data local inpath '/export/servers/hivedatas/score.csv'intotable score;
hive 语句综合操作
1、查询姓氏首字母为”M” 的教练的数量
selectcount(t_id) from teacher where t_name like'M%';
2、查询学过”MILLER” 教练授课的同学的信息
select stu.*from student stu,score sc where stu.s_id = sc.s_id and sc.c_id in (select c_id from course c, teacher t where t.t_id = c.t_id and t_name ='MILLER');
3、查询所有同学的学生编号、学生姓名、选课总数、所有运动的总成绩
select stu.s_id,stu.s_name,count(sc.c_id) sum_course,sum(sc.s_score) sum_score from student stu leftjoin score sc on stu.s_id = sc.s_id groupby stu.s_id,stu.s_name;
4、查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select stu.s_id,stu.s_name,avg(sc.s_score) avg_score from student stu leftjoin score sc on stu.s_id = sc.s_id groupby stu.s_id,stu.s_name having avg_score >=60; select stu.s_id,stu.s_name,Round(avg(sc.s_score),2) avg_score from student stu leftjoin score sc on stu.s_id = sc.s_id groupby stu.s_id,stu.s_name having Round(avg(sc.s_score),2) >=60;
5、查询平均成绩小于 60 分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)
select stu.s_id,stu.s_name,avg(sc.s_score) avg_score from student stu leftjoin score sc on stu.s_id = sc.s_id groupby stu.s_id,stu.s_name having avg_score <60; select stu.s_id,stu.s_name,0 avg_score from student stu where stu.s_id notin(selectdistinct sc.s_id from score sc); select stu.s_id,stu.s_name,avg(sc.s_score) avg_score from student stu leftjoin score sc on stu.s_id = sc.s_id groupby stu.s_id,stu.s_name having avg_score <60unionallselect stu.s_id,stu.s_name,0 avg_score from student stu where stu.s_id notin(selectdistinct sc.s_id from score sc);
6、查询学过编号为”01” 并且也学过编号为”02” 的运动的同学的信息
select stu.*from student stu,score sc,score sc2 where stu.s_id = sc.s_id and stu.s_id = sc2.s_id and sc.c_id ='01'and sc2.c_id ='02';
7、查询男生、女生人数
selectcount(s_sex='female'),count(s_sex='male') from student;
8、查询不及格的课程,并按课程号从大到小排列
select sc.c_id,sc.s_score from score sc where sc.s_score<60orderby sc.c_id desc;
9、查询课程编号为”01” 且课程成绩在 60 分以上的学生的学号和姓名;
select stu.s_id,stu.s_name,sc.s_score,sc.c_id from score sc,student stu where sc.s_id=stu.s_id and sc.c_id =01and sc.s_score>60;
10、查询不及格的课程,并按课程号从大到小排列
select sc.c_id,sc.s_score FROM score sc WHERE sc.s_score<60orderby sc.c_id desc;