hive簡單查詢

select完整格式

  • 完整格式如下
select 
[distinct] 字段名
from 表名
where 查詢條件
group by 分組字段
having 篩選條件
order by 排序字段
limit 限制條件
  • 查詢順序爲
from       -- 拿到表的數據
where      -- 對根據條件表數據進行篩選
group by     -- 根據某個字段進行分組
having       -- 對分組之後的數據進行再次篩選
order by     -- 對數據排序(排序不止有order by)
limit        -- 限制數據條數
distinct     -- 對數據去重
             -- 執行聚合函數對結果進行聚合
select       -- 查詢的結果(select結果的本質是一張虛擬表)

單表查詢

  • 基本查詢
//查詢時重命名列
select s_id as myid ,c_id from score;
//字段去重查詢
select distinct type from temp;
//查詢表中所有記錄的name和數學英語的總分
select name,math+english from 表名; 
  • where條件查詢
select * from score where s_score > 60; 查詢出分數大於60的數據
select * from student where age>=20 && age<=30; 查詢年齡在2030之間的所有學生
select * from student where age>=20 and age<=30; 查詢年齡在2030之間的所有學生
select * from student where age in (18,19,20); 查詢年齡爲18 19 20歲所有學生
select * from student where age=18 or age=19 or age=20; 查詢年齡爲18 19 20歲所有學生
select * from student where age is not null; 查詢年齡字段不爲空的所有學生
select * from student where name like "馬%"; 查詢姓馬的所有學生
select * from student where name like "_化%"; 查詢名字第二個字爲化的所有學生
select * from score where s_score >80 and s_id = '01'; 查詢成績大於80,並且s_id是01的數據 
select * from score where s_score > 80 or s_id = '01'; 查詢成績大於80,或者s_id 是01的數 
  • 簡單排序查詢
select * from student order by math; 查詢所有學生並按數學成績排序,默認是升序
select * from student order by math desc; 查詢所有學生並按數學成績排序,並指定降序排列
  • 聚合函數查詢 (多進一出的函數)
select count(id) from student; 查詢學生的總人數
select max(math),min(math) from student; 查詢數學成績的最大值和最小值
select sum(math),avg(math) from student; 對數學成績進行求和,求平均值
  • 分組查詢 group by
分別查詢所有男生的數學平均分和所有女生的數學平均分
select sex,avg(math) from student group by sex; 
分別查詢男女學生數學平均分,成績小於70的學生不參與計算
select sex,avg(math) from student where math>70 group by sex; 
分別查詢男女學生的總人數
select sex,count(id) from student group by sex; 
  • having查詢
    having用於對分組後的數據過濾
    求平均分數大於85的學生信息
select s_id ,avg(s_score) as avgscore from score group by s_id having avgscore > 85;
  • LIMIT語句
select * from score limit 3; // 限制返回三條記錄
select * from student limit (x,y);  //從索引x開始顯示y條數據  索引從0開始
  • union語句
    Union:對兩個結果集進行並集操作,不包括重複行,同時進行默認規則的排序;
    Union All:對兩個結果集進行並集操作,包括重複行,不進行排序;
  • 嵌套查詢
    可以將一個select的結果作爲中間虛擬表,提供給另一個select進行查詢
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章