转载:Hive sql语句必练50题-入门到精通(2)

原始链接:https://blog.csdn.net/Thomson617/article/details/83280617

 

Hive sql语句必练50题-入门到精通(2)

 

承接: Hive sql语句必练50题-入门到精通(1)

– 21、查询不同老师所教不同课程平均分从高到低显示:
– 方法1


   
   
  1. select course.c_id,course.t_id,t_name,round(avg(s_score), 2) as avgscore from course
  2. join teacher on teacher.t_id=course.t_id
  3. join score on course.c_id=score.c_id
  4. group by course.c_id,course.t_id,t_name order by avgscore desc;

– 方法2


   
   
  1. select course.c_id,course.t_id,t_name,round(avg(s_score), 2) as avgscore from course,teacher,score
  2. where teacher.t_id=course.t_id and course.c_id=score.c_id
  3. group by course.c_id,course.t_id,t_name order by avgscore desc;

– 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩:


   
   
  1. select tmp1. * from
  2. ( select * from score where c_id = '01' order by s_score desc limit 3)tmp1
  3. order by s_score asc limit 2
  4. union all select tmp2. * from
  5. ( select * from score where c_id = '02' order by s_score desc limit 3)tmp2
  6. order by s_score asc limit 2
  7. union all select tmp3. * from
  8. ( select * from score where c_id = '03' order by s_score desc limit 3)tmp3
  9. order by s_score asc limit 2;

– 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比


   
   
  1. select c.c_id,c.c_name,tmp1.s0_60, tmp1.percentum,tmp2.s60_70, tmp2.percentum,tmp3.s70_85, tmp3.percentum,tmp4.s85_100, tmp4.percentum
  2. from course c
  3. join( select c_id,sum( case when s_score< 60 then 1 else 0 end ) as s0_60,
  4. round( 100*sum( case when s_score< 60 then 1 else 0 end )/count(c_id), 2) as percentum
  5. from score group by c_id)tmp1 on tmp1.c_id =c.c_id
  6. left join( select c_id,sum( case when s_score< 70 and s_score>= 60 then 1 else 0 end ) as s60_70,
  7. round( 100*sum( case when s_score< 70 and s_score>= 60 then 1 else 0 end )/count(c_id), 2) as percentum
  8. from score group by c_id)tmp2 on tmp2.c_id =c.c_id
  9. left join( select c_id,sum( case when s_score< 85 and s_score>= 70 then 1 else 0 end ) as s70_85,
  10. round( 100*sum( case when s_score< 85 and s_score>= 70 then 1 else 0 end )/count(c_id), 2) as percentum
  11. from score group by c_id)tmp3 on tmp3.c_id =c.c_id
  12. left join( select c_id,sum( case when s_score>= 85 then 1 else 0 end ) as s85_100,
  13. round( 100*sum( case when s_score>= 85 then 1 else 0 end )/count(c_id), 2) as percentum
  14. from score group by c_id)tmp4 on tmp4.c_id =c.c_id;

– 24、查询学生平均成绩及其名次:


   
   
  1. select tmp. *, row_number() over( order by tmp.avgScore desc) Ranking from
  2. ( select student.s_id,
  3. student.s_name,
  4. round( avg(score.s_score), 2) as avgScore
  5. from student join score
  6. on student.s_id =score.s_id
  7. group by student.s_id,student.s_name)tmp
  8. order by avgScore desc;

– 25、查询各科成绩前三名的记录

–课程id为01的前三名


   
   
  1. select score.c_id,course.c_name,student.s_name,s_score from score
  2. join student on student.s_id=score.s_id
  3. join course on score.c_id= '01' and course.c_id=score.c_id
  4. order by s_score desc limit 3;

–课程id为02的前三名


   
   
  1. select score.c_id,course.c_name,student.s_name,s_score
  2. from score
  3. join student on student.s_id=score.s_id
  4. join course on score.c_id= '02' and course.c_id=score.c_id
  5. order by s_score desc limit 3;

–课程id为03的前三名


   
   
  1. select score.c_id,course.c_name,student.s_name,s_score
  2. from score
  3. join student on student.s_id=score.s_id
  4. join course on score.c_id= '03' and course.c_id=score.c_id
  5. order by s_score desc limit 3;

– 26、查询每门课程被选修的学生数:


   
   
  1. select c.c_id,c.c_name,tmp.number from course c
  2. join ( select c_id,count( 1) as number from score
  3. where score.s_score< 60 group by score.c_id)tmp
  4. on tmp.c_id=c.c_id;

– 27、查询出只有两门课程的全部学生的学号和姓名:


   
   
  1. select st.s_id,st.s_name from student st
  2. join ( select s_id from score group by s_id having count(c_id) = 2)tmp
  3. on st.s_id =tmp.s_id;

– 28、查询男生、女生人数:


   
   
  1. select tmp1.man,tmp2. women from
  2. ( select count(1) as man from student where s_sex= '男')tmp1,
  3. ( select count(1) as women from student where s_sex= '女')tmp2;

– 29、查询名字中含有"风"字的学生信息:

select * from student where s_name like '%风%';

   
   

– 30、查询同名同性学生名单,并统计同名人数:


   
   
  1. select s1.s_id,s1.s_name,s1.s_sex,count(*) as sameName
  2. from student s1,student s2
  3. where s1.s_name=s2.s_name and s1.s_id<>s2.s_id and s1.s_sex=s2.s_sex
  4. group by s1.s_id,s1.s_name,s1.s_sex;

– 31、查询1990年出生的学生名单:

select * from student where s_birth like '1990%';

   
   

– 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:


   
   
  1. select score.c_id,c_name,round(avg(s_score), 2) as avgScore from score
  2. join course on score.c_id=course.c_id
  3. group by score.c_id,c_name order by avgScore desc,score.c_id asc;

– 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:


   
   
  1. select score.s_id,s_name,round(avg(s_score), 2) as avgScore from score
  2. join student on student.s_id=score.s_id
  3. group by score.s_id, s_name having avg(s_score) >= 85;

– 34、查询课程名称为"数学",且分数低于60的学生姓名和分数:


   
   
  1. select s_name,s_score as mathScore from student
  2. join ( select s_id,s_score
  3. from score,course
  4. where score.c_id=course.c_id and c_name= '数学')tmp
  5. on tmp.s_score < 60 and student.s_id=tmp.s_id;

– 35、查询所有学生的课程及分数情况:


   
   
  1. select a.s_name,
  2. SUM( case c.c_name when '语文' then b.s_score else 0 end ) as chainese,
  3. SUM( case c.c_name when '数学' then b.s_score else 0 end ) as math,
  4. SUM( case c.c_name when '英语' then b.s_score else 0 end ) as english,
  5. SUM(b.s_score) as sumScore
  6. from student a
  7. join score b on a.s_id =b.s_id
  8. join course c on b.c_id =c.c_id
  9. group by s_name,a.s_id;

后续部分参见:
https://blog.csdn.net/Thomson617/article/details/83281254

Hive下的SQL经验总结:


   
   
  1. ( 1).不支持非等值连接,一般使用 left joinright join 或者 inner join替代。
  2. SQL中对两表内联可以写成:
  3. select * from dual a,dual b where a.key = b.key;
  4. •Hive中应为:
  5. select * from dual a join dual b on a.key = b.key;
  6. 而不是传统的格式:
  7. SELECT t1.a1 as c1, t2.b1 as c2 FROM t1, t2 WHERE t1.a2 = t2.b2
  8. ( 2).分号字符:不能智能识别concat(‘;’,key),只会将‘;’当做 SQL结束符号。
  9. •分号是 SQL语句结束标记,在HiveQL中也是,但是在HiveQL中,对分号的识别没有那么智慧,例如:
  10. select concat(key,concat( ';',key)) from dual;
  11. •但HiveQL在解析语句时提示:
  12. FAILED: Parse Error: line 0: -1 mismatched input '<EOF>' expecting ) in function specification
  13. •解决的办法是,使用分号的八进制的ASCII码进行转义,那么上述语句应写成:
  14. select concat(key,concat( '\073',key)) from dual;
  15. ( 3).不支持 INSERT INTOValues(), UPDATE, DELETE等操作.这样的话,就不要很复杂的锁机制来读写数据。
  16. INSERT INTO syntax is only available starting in version 0.8INSERT INTO就是在表或分区中追加数据。
  17. ( 4).HiveQL中String类型的字段若是空( empty)字符串, 即长度为 0, 那么对它进行 IS NULL的判断结果是 False,使用 left join可以进行筛选行。
  18. ( 5).不支持 ‘ < dt <’这种格式的范围查找,可以用dt in(”,”)或者 between替代。
  19. ( 6).Hive不支持将数据插入现有的表或分区中,仅支持覆盖重写整个表,示例如下:
  20. INSERT OVERWRITE TABLE t1 SELECT * FROM t2;
  21. ( 7). group by的字段,必须是 select后面的字段, select后面的字段不能比 group by的字段多.
  22. 如果 select后面有聚合函数,则该 select语句中必须有 group by语句;
  23. 而且 group by后面不能使用别名;
  24. 有聚合函数存在就必须有 group by.
  25. ( 8). select , wherehaving 之后不能跟子查询语句(一般使用 left joinright join 或者 inner join替代)
  26. ( 9).先 join(及 inner join) 然后 left joinright join
  27. ( 10).hive不支持group_concat方法,可用 concat_ws( '|', collect_set(str)) 实现
  28. ( 11). not in<> 不起作用,可用 left join tmp on tableName.id = tmp.id where tmp.id is null 替代实现
  29. ( 12).hive 中‘不等于’不管是用! 或者 <>符号实现,都会将空值即 null过滤掉,此时要用
  30. where (white_level <> '3' or white_level is null
  31. 或者 where (white_level != '3' or white_level is null ) 来保留 null 的情况。
  32. ( 13). union all 后面的表不加括号,不然执行报错;
  33. hive也不支持顶层的 union all,使用子查询来解决;
  34. union all 之前不能有DISTRIBUTE BY | SORT BY | ORDER BY | LIMIT 等查询条件
  •  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值