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

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

 

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

 

承接: Hive sql语句必练50题-入门到精通(2)
– 36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:


   
   
  1. select student.s_id,s_name,c_name, s_score from student
  2. join ( select sc.* from score sc
  3. left join(select s_id from score where s_score < 70 group by s_id)tmp
  4. on sc.s_id=tmp.s_id where tmp.s_id is null)tmp2
  5. on student.s_id=tmp2.s_id
  6. join course on tmp2.c_id=course.c_id
  7. order by s_id;
  8. **-- 查询全部及格的信息**
  9. select sc.* from score sc
  10. left join(select s_id from score where s_score < 60 group by s_id)tmp
  11. on sc.s_id=tmp.s_id
  12. where tmp.s_id is null;
  13. **-- 或(效率低)**
  14. select sc.* from score sc
  15. where sc. s_id not in (select s_id from score where s_score < 60 group by s_id);

– 37、查询课程不及格的学生:


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

–38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:


   
   
  1. select student.s_id,s_name,s_score as score_01
  2. from student
  3. join score on student.s_id=score.s_id
  4. where c_id= '01' and s_score >= 80;

– 39、求每门课程的学生人数:


   
   
  1. select course.c_id,course.c_name,count( 1) as selectNum
  2. from course
  3. join score on course.c_id=score.c_id
  4. group by course.c_id,course.c_name;

– 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:


   
   
  1. select student.*,tmp3.c_name,tmp3.maxScore
  2. from ( select s_id,c_name,max(s_score) as maxScore from score
  3. join ( select course.c_id,c_name from course join
  4. ( select t_id,t_name from teacher where t_name= '张三')tmp
  5. on course.t_id=tmp.t_id)tmp2
  6. on score.c_id=tmp2.c_id group by score.s_id,c_name
  7. order by maxScore desc limit 1)tmp3
  8. join student
  9. on student.s_id=tmp3.s_id;
  • 同课程成绩相同的学生的学生编号、课程编号、学生成绩:

   
   
  1. select distinct a.s_id,a.c_id,a.s_score from score a,score b
  2. where a.c_id <> b.c_id and a.s_score =b.s_score;

– 42、查询每门课程成绩最好的前三名:


   
   
  1. select tmp1. * from
  2. ( select *, row_number() over( order by s_score desc) ranking
  3. from score where c_id = '01')tmp1
  4. where tmp1.ranking <= 3
  5. union all
  6. select tmp2. * from
  7. ( select *, row_number() over( order by s_score desc) ranking
  8. from score where c_id = '02')tmp2
  9. where tmp2.ranking <= 3
  10. union all
  11. select tmp3. * from
  12. ( select *, row_number() over( order by s_score desc) ranking
  13. from score where c_id = '03')tmp3
  14. where tmp3.ranking <= 3;

– 43、统计每门课程的学生选修人数(超过5人的课程才统计):
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列


   
   
  1. select distinct course.c_id,tmp.num from course
  2. join ( select c_id, count( 1) as num from score group by c_id)tmp
  3. where tmp.num >= 5 order by tmp.num desc ,course.c_id asc;

– 44、检索至少选修两门课程的学生学号:


   
   
  1. select s_id, count(c_id) as totalCourse
  2. from score
  3. group by s_id
  4. having count(c_id) >= 2;

– 45、查询选修了全部课程的学生信息:


   
   
  1. select student.*
  2. from student,
  3. ( select s_id,count(c_id) as totalCourse
  4. from score group by s_id)tmp
  5. where student.s_id=tmp.s_id and totalCourse= 3;

–46、查询各学生的年龄(周岁):
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

方法一


   
   
  1. select s_name,s_birth,
  2. ( year( CURRENT_DATE) - year(s_birth) -
  3. ( case when month( CURRENT_DATE) < month(s_birth) then 1
  4. when month( CURRENT_DATE) = month(s_birth) and day( CURRENT_DATE) < day(s_birth) then 1
  5. else 0 end)
  6. ) as age
  7. from student;

方法二:


   
   
  1. select s_name,s_birth,
  2. floor((datediff( current_date,s_birth) - floor(( year( current_date) - year(s_birth)) / 4)) / 365) as age
  3. from student;

说明:floor((year(current_date) - year(s_birth))/4  考虑闰年的情况,减去闰年多出来的那一天。

– 47、查询本周过生日的学生:
–方法1

备注:原作者写错了,我更正了一下。

select * from student where weekofyear(CURRENT_DATE) =weekofyear(s_birth);

   
   

 

–方法2

备注:原作者将今天定义为10月14日,相当于写死了日期。


   
   
  1. select s_name,s_sex,s_birth from student
  2. where substring(s_birth, 6, 2) = '10'
  3. and substring(s_birth, 9, 2) = 14;

– 48、查询下周过生日的学生:
–方法1

select * from student where weekofyear(CURRENT_DATE)+1 =weekofyear(s_birth);

   
   

–方法2


   
   
  1. select s_name,s_sex,s_birth from student
  2. where substring(s_birth, 6, 2) = '10'
  3. and substring(s_birth, 9, 2) >= '15'
  4. and substring(s_birth, 9, 2) <= '21';

– 49、查询本月过生日的学生:
–方法1

select * from student where MONTH(CURRENT_DATE) =MONTH(s_birth);

   
   

–方法2

select s_name,s_sex,s_birth from student where substring(s_birth,6,2)='10';

   
   

– 50、查询12月份过生日的学生:

select s_name,s_sex,s_birth from student where substring(s_birth,6,2)='12';

   
   

所有代码亲测有效!
如果因为hive版本及测试环境造成无法运行的还请自行修正!

hive sql中的部分方法总结:


   
   
  1. 1. case when ... then ... else ... end
  2. 2.length(string)
  3. 3. cast(string as bigint)
  4. 4.rand() 返回一个 01范围内的随机数
  5. 5. ceiling( double) 向上取整
  6. 6.substr(string A, int start, int len)
  7. 7.collect_set(col)函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生 array类型字段
  8. 8.concat()函数
  9. 1、功能:将多个字符串连接成一个字符串。
  10. 2、语法:concat(str1, str2,...)
  11. 返回结果为连接参数产生的字符串,如果有任何一个参数为 null,则返回值为 null
  12. 9.concat_ws()函数
  13. 1、功能:和concat()一样,将多个字符串连接成一个字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)
  14. 2、语法:concat_ws(separator, str1, str2, ...)
  15. 说明:第一个参数指定分隔符。需要注意的是分隔符不能为 null,如果为 null,则返回结果为 null
  16. 10.nvl(expr1, expr2):空值转换函数 nvl(x,y) Returns y if x is null else return x
  17. 11.if( boolean testCondition, T valueTrue, T valueFalse)
  18. 12. row_number() over()分组排序功能, over()里头的分组以及排序的执行晚于 where group by order by 的执行。
  19. 13.获取年、月、日、小时、分钟、秒、当年第几周
  20. select
  21. year( '2018-02-27 10:00:00') as year
  22. , month( '2018-02-27 10:00:00') as month
  23. , day( '2018-02-27 10:00:00') as day
  24. , hour( '2018-02-27 10:00:00') as hour
  25. , minute( '2018-02-27 10:00:00') as minute
  26. , second( '2018-02-27 10:00:00') as second
  27. ,weekofyear( '2018-02-27 10:00:00') as weekofyear
  28. 获取当前时间:
  29. 1). current_timestamp
  30. 2).unix_timestamp()
  31. 3).from_unixtime(unix_timestamp())
  32. 4). CURRENT_DATE
  •  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值