案例6-1 比較運算符應用
1.需求 創建學生emp外部表,向外部表中導入數據,並應用比較運算符做簡單查詢。
2.數據準備 (1)原始數據 emp表中數據見表6-1。
18999065 王述龍 男 1998-12-10 上海 98 100 2000
18007066 孫宇鵬 男 1999-11-17 瀋陽 51 500
18999141 王應龍 男 2000-02-04 瀋陽 59 100
18008158 張瓊宇 女 1999-07-01 大連 89 200
18999063 宋傳涵 女 1999-07-20 上海 86 100 1000
18008009 李亞楠 女 1998-01-24 杭州 97 200 2000
18008026 侯楠楠 男 2000-01-29 北京 79 200
18008027 陳姝元 女 1999-06-24 北京 96 200 1500
18009183 陸春宇 男 1998-01-18 瀋陽 87 300 1000
18009173 孫雲琳 女 1997-07-15 上海 56 300
18008014 尤騫梓 女 1999-04-25 杭州 86 200 1000
18998002 張愛林 男 1999-05-16 北京 92 400 1500
18009019 曹雪東 男 2000-11-20 北京 78 300
18998153 賈芸梅 女 2000-06-12 大連 88 400 1000
18007051 温勇元 男 1999-08-08 上海 65 500
18998039 張微微 女 1998-01-27 北京 90 400 1500
18007063 李君年 男 1998-03-21 上海 78 500
18007095 盧昱澤 女 1998-08-01 上海 57 500
18007096 趙旭輝 男 1999-02-18 北京 75 500
18009087 張矗年 男 1997-07-26 重慶 86 300 1000
(2)創建本地數據文件emp.txt vim emp.txt 並將表6-1中數據導入其中,然後保存並退出。
3.Hive實例操作
(1)創建外部表emp
create external table if not exists emp(
empno int,
ename string,
gender string,
bday string,
area string,
score double,
deptno int,
scholarship double)
row format delimited fields terminated by '\t';
(1.1)創建dept表
CREATE TABLE dept(
deptno INT,
deptname STRING,
buildingno STRING
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;
(2)向外部表emp中導入數據
load data local inpath '/opt/datas/emp.txt' into table emp;
load data local inpath '/opt/datas/dept.txt' into table dept;
(3)設置漢字編碼,否則漢字出現亂碼
alter table emp set serdeproperties('serialization.encoding’=‘utf-8’);
(4)查詢成績為98分的學生的所有信息
select empno,ename,gender,bday,area,score,deptno from emp where score =98;
(5)查詢成績為90分到100分的學生信息
select empno,ename,gender,area,score,deptno from emp where score between 90 and 100;
(6)查詢獎學金scholarship不為空的所有學生信息
select empno,ename,gender,area,score,scholarship from emp where scholarship is not null;
(7)查詢成績為86分或96分的學生信息
hive(hivedwh)>select ename,gender,area,score,deptno from emp where score IN (86, 96);
(8)查詢成績大於90分的所有學生
select empno,ename,gender,bday,area,score,deptno from emp where score >90;
1.查詢成績以9開頭的學生信息
selectempno,ename,gender,bday,area,score from emp where score LIKE '9%';
2.查詢成績中第二個數值為8的學生信息
select ename,area,score,deptno,scholarship from emp where score LIKE '_8%';
3.查詢出生日期中含有6的學生信息,正則表達式僅限於字符串類型的字段
select empno,ename,bday,area,score,deptno from emp where bday RLIKE '[6]';
案例6-3 邏輯運算符應用
1.查詢成績大於80分,系別是300的學生信息
select empno,ename,bday,area,score,deptno
from emp
where score>80 and deptno=300;
2.查詢成績大於95分,或者系別是100的學生信息
select ename,gender,bday,area,score,deptno from emp where score>95 or deptno=100;
3.查詢除系別200和系別500外的學生信息
hive(hivedwh)>select empno,ename,bday,area,score,deptno from emp where deptno not IN(200, 500);
案例6-4 Group By語句應用
1.查詢emp表每個部門的平均成績
select deptno, avg(score) avg_score from emp group by deptno;
2.查詢emp表每個部門中男女性別的最好成績
select deptno, gender, max(score) max_score from emp group by deptno, gender;
Hive實例操作
1.查詢emp表中平均成績大於80分的部門:
select deptno, avg(score) avg_score from emp group by deptno having avg_score > 80;
6.4.1 等值連接
SELECT e.empno, e.ename, d.deptno, d.deptname
FROM emp
e JOIN dept d ON e.deptno = d.deptno;
使用表的別名可以簡化查詢,還可以提高執行效率。 例如,合併dept表和emp表:
select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
6.4.4 左外連接Left Outer Join
SELECT e.empno, e.ename, e.deptno
FROM empe LEFT SEMI JOIN dept d ON e.deptno = d.deptno;