Hive sql語句必練50題-入門到精通(3)(轉載)

Hive sql語句必練50題-入門到精通(3)

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

原創Thomson617 最後發佈於2018-10-23 00:50:59 閱讀數 1945  收藏
展開
承接: Hive sql語句必練50題-入門到精通(2)
– 36、查詢任何一門課程成績在70分以上的學生姓名、課程名稱和分數:

select student.s_id,s_name,c_name,s_score from student
  join (select sc.* from score sc
        left join(select s_id from score where s_score < 70 group by s_id)tmp
        on sc.s_id=tmp.s_id where tmp.s_id is null)tmp2
    on student.s_id=tmp2.s_id
  join course on tmp2.c_id=course.c_id
order by s_id;


**-- 查詢全部及格的信息**
select sc.* from score sc
  left join(select s_id from score where s_score < 60 group by s_id)tmp
    on sc.s_id=tmp.s_id
where  tmp.s_id is  null;
**-- 或(效率低)**
select sc.* from score sc
where sc.s_id not in (select s_id from score where s_score < 60 group by s_id);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
– 37、查詢課程不及格的學生:

select s_name,c_name as courseName,tmp.s_score
from student
join (select s_id,s_score,c_name
      from score,course
      where score.c_id=course.c_id and s_score < 60)tmp
on student.s_id=tmp.s_id;
1
2
3
4
5
6
–38、查詢課程編號爲01且課程成績在80分以上的學生的學號和姓名:

select student.s_id,s_name,s_score as score_01
from student
join score on student.s_id=score.s_id
where c_id='01' and s_score >= 80;
1
2
3
4
– 39、求每門課程的學生人數:

select course.c_id,course.c_name,count(1)as selectNum
from course
join score on course.c_id=score.c_id
group by course.c_id,course.c_name;
1
2
3
4
– 40、查詢選修"張三"老師所授課程的學生中,成績最高的學生信息及其成績:

select student.*,tmp3.c_name,tmp3.maxScore
from (select s_id,c_name,max(s_score)as maxScore from score
      join (select course.c_id,c_name from course join
                  (select t_id,t_name from teacher where t_name='張三')tmp
            on course.t_id=tmp.t_id)tmp2
      on score.c_id=tmp2.c_id group by score.s_id,c_name
      order by maxScore desc limit 1)tmp3
join student
on student.s_id=tmp3.s_id;
1
2
3
4
5
6
7
8
9
– 41、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績:

select distinct a.s_id,a.c_id,a.s_score from score a,score b
    where a.c_id <> b.c_id and a.s_score=b.s_score;
1
2
– 42、查詢每門課程成績最好的前三名:

select tmp1.* from
  (select *,row_number()over(order by s_score desc) ranking
      from score  where c_id ='01')tmp1
where tmp1.ranking <= 3
union all
select tmp2.* from
  (select *,row_number()over(order by s_score desc) ranking
      from score where c_id ='02')tmp2
where tmp2.ranking <= 3
union all
select tmp3.* from
  (select *,row_number()over(order by s_score desc) ranking
      from score where c_id ='03')tmp3
where tmp3.ranking <= 3;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
– 43、統計每門課程的學生選修人數(超過5人的課程才統計):
– 要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列

select distinct course.c_id,tmp.num from course
    join (select c_id,count(1) as num from score group by c_id)tmp
    where tmp.num>=5 order by tmp.num desc ,course.c_id asc;
1
2
3
– 44、檢索至少選修兩門課程的學生學號:

select s_id,count(c_id) as totalCourse
from score
group by s_id
having count(c_id) >= 2;
1
2
3
4
– 45、查詢選修了全部課程的學生信息:

select student.* 
from student,
     (select s_id,count(c_id) as totalCourse 
      from score group by s_id)tmp
where student.s_id=tmp.s_id and totalCourse=3;
1
2
3
4
5
–46、查詢各學生的年齡(週歲):
– 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一
方法一

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

select s_name,s_birth,
    floor((datediff(current_date,s_birth) - floor((year(current_date) - year(s_birth))/4))/365) as age
from student;
1
2
3
– 47、查詢本週過生日的學生:
–方法1

select * from student where weekofyear(CURRENT_DATE)+1 =weekofyear(s_birth);
1
–方法2

select s_name,s_sex,s_birth from student
    where substring(s_birth,6,2)='10'
    and substring(s_birth,9,2)=14;
1
2
3
– 48、查詢下週過生日的學生:
–方法1

select * from student where weekofyear(CURRENT_DATE)+1 =weekofyear(s_birth);
1
–方法2

select s_name,s_sex,s_birth from student
    where substring(s_birth,6,2)='10'
    and substring(s_birth,9,2)>=15
    and substring(s_birth,9,2)<=21;
1
2
3
4
– 49、查詢本月過生日的學生:
–方法1

select * from student where MONTH(CURRENT_DATE) =MONTH(s_birth);
1
–方法2

select s_name,s_sex,s_birth from student where substring(s_birth,6,2)='10';
1
– 50、查詢12月份過生日的學生:

select s_name,s_sex,s_birth from student where substring(s_birth,6,2)='12';
1
所有代碼親測有效!
如果因爲hive版本及測試環境造成無法運行的還請自行修正!

hive sql中的部分方法總結:

1.case when ... then ... else ... end

2.length(string)

3.cast(string as bigint)

4.rand()       返回一個0到1範圍內的隨機數

5.ceiling(double)    向上取整

6.substr(string A, int start, int len)

7.collect_set(col)函數只接受基本數據類型,它的主要作用是將某字段的值進行去重彙總,產生array類型字段

8.concat()函數
    1、功能:將多個字符串連接成一個字符串。
    2、語法:concat(str1, str2,...)
    返回結果爲連接參數產生的字符串,如果有任何一個參數爲null,則返回值爲null。

    9.concat_ws()函數
    1、功能:和concat()一樣,將多個字符串連接成一個字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)
    2、語法:concat_ws(separator, str1, str2, ...)
    說明:第一個參數指定分隔符。需要注意的是分隔符不能爲null,如果爲null,則返回結果爲null。

    10.nvl(expr1, expr2):空值轉換函數  nvl(x,y)    Returns y if x is null else return x

11.if(boolean testCondition, T valueTrue, T valueFalse)

12.row_number()over()分組排序功能,over()裏頭的分組以及排序的執行晚於 where group by  order by 的執行。

13.獲取年、月、日、小時、分鐘、秒、當年第幾周
    select 
        year('2018-02-27 10:00:00')       as year
        ,month('2018-02-27 10:00:00')      as month
        ,day('2018-02-27 10:00:00')        as day
        ,hour('2018-02-27 10:00:00')       as hour
        ,minute('2018-02-27 10:00:00')     as minute
        ,second('2018-02-27 10:00:00')     as second
        ,weekofyear('2018-02-27 10:00:00') as weekofyear
  獲取當前時間:
        1).current_timestamp
        2).unix_timestamp()
        3).from_unixtime(unix_timestamp())
        4).CURRENT_DATE
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章