單純的練習,是從尚硅谷大數據課程之Hive(2019新版)學的,反正我記錄的都是我手敲過的。。。
1.空字段賦值
函數說明:
NVL:給值為null的數據賦值。格式是NVL(string1 , replace with)。如果string1為null,則NVL函數返回replace_with的值,否則返回string1的值。如果兩個參數都為null,則返回null
例如:
> select nvl(comm,0) from emp;
2.時間類
1.date_format 格式化時間
> select date_format('1985-5-23','yyyy-MM-dd');
2.date_add 時間跟天數相加
> select date_add('1985-5-23',6);
可以填負值
3.date_sub 時間跟天數相減
> select date_sub('1985-5-23',6);
4.datediff 兩天相減
> select datediff('1985-05-23','1985-05-26');
3.CASE WHEN
1.數據準備
name dept_id sex
悟空 A 男
大海 A 男
松松 B 男
鳳姐 A 女
婷姐 B 女
婷婷 B 女
2.需求
求出不同部門男女各多少人。結果要求:
A 2 1
B 1 2
3.創建本地emp_sex.txt導入數據
> create table emp_sex( name string, dept_id string, sex string) row format delimited fields terminated by '\t';
> load data local inpath '/opt/soft/files/emp_sex.txt' into table emp_sex;
4.按需求查數據
> select dept_id, sum (case sex when '男' then 1 else 0 end) male_count, sum (case sex when '女' then 1 else 0 end) female_count from emp_sex group by dept_id;
select dept_id, sum (sum(sex='男',1,0)) male_count, sum (sum(sex='女',1,0) female_count from emp_sex group by dept_id;
4.行轉列
1.函數說明:
concat(string A/col,string B/col...):返回輸入字符串連接后的結果,支持任意個輸入字符串。
concat_ws(separator,str1,str2...):它是一個特殊形式的concat(),第一個參數剩余參數之間的分隔符。分隔符可以是與剩余參數一樣的字符串。如果分隔符是null,返回值也會是null。這個函數會跳過分隔符參數后的任何null和空字符串。分隔符將會被加到被連接的字符串之間;
concat_set(col):函數只接受基本數據類型,主要功能是將某字段的值進行去重,匯總,產生arry類型字段。
數據準備:
name constellation blood_type
孫悟空 白羊座 A
大海 射手座 A
松松 白羊座 B
豬八戒 白羊座 A
鳳姐 射手座 A
2.需求
星座血型一樣的人歸類到一起。結果要求:
射手座,A 大海|鳳姐
白羊座,A 孫悟空|豬八戒
白羊座,B 松松
3.創建本地數據constellation.txt
create table person_info( name string, constellation string, blood_type string) row format delimited fields terminated by '\t';
load data local inpath "/opt/soft/files/constellation.txt" into table person_info;
4.查
select constellation_blood_type,concat_ws('|',collect_set(name)) from (select concat(constellation,',',blood_type) constellation_blood_type, name from person_info) t1 group by constellation_blood_type;
5.列轉行
1.函數說明
EXPLODE(col):將hive一列中復雜的array或者map結構拆分成多行。
LATERAL VIEW
用法:LATERAL VIEW udtf(expression) tableAlias AS columnAlias
解釋:用戶和split,explode等UDTF一起使用,能將一列數據拆成多行數據,在此基礎上可以做數據的聚合。
2.需求
電影如下:
movie category
《疑犯追蹤》 懸疑,動作,科幻,劇情
《Lie to me》 懸疑,警匪,動作,心理,劇情
《戰狼》 戰爭,動作,災難
我們要把電影分類的數組數據展開,結果如下:
《疑犯追蹤》 懸疑
《疑犯追蹤》 動作
《疑犯追蹤》 科幻
《疑犯追蹤》 劇情
《Lie to me》 懸疑
《Lie to me》 警匪
《Lie to me》 動作
《Lie to me》 心理
《Lie to me》 劇情
《戰狼》 戰爭
《戰狼》 動作
《戰狼》 災難
3.創建本地
create table movie_info( > movie string, > category array<string>) > row format delimited fields terminated by '\t' > collection items terminated by ',';
load data local inpath "/opt/soft/files/movie.txt" into table movie_info;
4.查
select movie,category_name from movie_info lateral view explode(category) table_tmp as category_name;
6.窗口函數
1.相關函數
OVER():指定的分析函數工作的數據窗口大小,這個數據窗口大小可能會隨著行的變化而變化。
CURRENT ROW:當前行
n PRECEDING:往前n行數據
n FOLLOWING:往后n行數據
UNBOUNDED:起點,UNBOUNDED PRECEDING 表示從前面的起點,UNBOUNDED FOLLOWING表示到后面的終點。
LAG(col,n):往前第n行數據。
LEAD(col,n):往后n行數據。
NTILE(n):把有序分區中的行分發到指定數據的組,各個組有編號,編號從1開始,對于每一行,NTILE返回此行所屬的組的編號,注意:n必須為int類型。
2.數據準備 需求
name orderdate cost
jack,2017-01-01,10
tony,2017-01-02,15
jack,2017-02-03,23
jack,2017-01-04,29
jack,2017-01-05,46
jack,2017-04-06,42
tony,2017-01-07,50
jack,2017-01-08,55
mart,2017-04-08,62
mart,2017-04-09,68
neil,2017-05-10,12
mart,2017-04-11,75
neil,2017-06-12,80
mart,2017-04-13,94
3.需求
1.查詢在2017年4月份購買過的顧客及總人數
select name,count(*) over() from business where substring(orderdate,1,7)='2017-04' group by name;
over()開窗函數,跟在聚合函數后面,等于開一部分數據集,專門給聚合函數使用
2.查詢顧客的購買明細及月購買總額
select * ,sum(cost) over() from business;
3.上述場景,要將cost按照日期進行累加
select name,orderdate,sum(cost) over(distribute by name sort by orderdate) from business;
4.查詢顧客上次的購買時間
select name,orderdate,cost,lag(orderdate,1,'1970-01-01') over(distribute by name sort by orderdate) from business;
7.RANK
1.函數說明
RANK() 排序相同會重復。總數不會變。
DENSE_RANK() 排序相同時會重復,總數會減少
ROW_NUMBER() 會根據順序計算
數據:
孫悟空 語文 87
孫悟空 數學 95
孫悟空 英語 68
大海 語文 94
大海 數學 56
大海 英語 84
松松 語文 64
松松 數學 86
松松 英語 84
婷婷 語文 65
婷婷 數學 85
婷婷 英語 78
看效果:
select name,subject,score, rank() over(partition by subject order by score desc) rank1,
dense_rank() over(partition by subject order by score desc) rank2,
row_number() over(partition by subject order by score desc) rank3
from score;
結果:
其他的結果就不展示了,因為我虛擬機搭的大數據集群是在是太慢了。。。
create語句和load也不詳細寫了
練習題
1.我們有如下用戶訪問數據
userId visitDate visitCount
u01 2019/1/21 5
u02 2019/1/23 6
u03 2019/1/22 8
u04 2019/1/20 3
u01 2019/1/23 6
u01 2019/2/21 8
u02 2019/1/23 6
u01 2019/2/22 4
要求使用SQL統計出每個用戶的累計訪問次數,如下表:
用戶 月份 小計 累計
u01 2017-01 11 11
u01 2017-02 12 23
u02 2017-01 12 12
u03 2017-01 8 8
u04 2017-01 3 3
創建表:
create table action(userId string,visitDate string,visitCount int) row format delimited fields terminated by "\t";
load data local inpath '/opt/soft/files/visit.txt' into table action;
解:
select userId, mn, sum_visitCount, sum(sum_visitCount) over(partition by userId order by mn) from (select userId, mn, sum(visitCount) sum_visitCount from (select userId, date_format(regexp_replace(visitDate,'/','-'),"yyyy-MM") mn, visitCount from action)t1 group by userId,mn) t2;
效果:
這個題有點難搞嗷!!
2.京東
有50w個京東店鋪,每個顧客訪問任何一個店鋪的任何一個商品時,都會產生一條訪問日志,訪問日志存儲的表名為visit,訪客用戶id為user_id,被訪問的店鋪名稱為shop。請統計:
數據:
u1 a
u2 b
u1 b
u1 a
u3 c
u4 b
u1 a
u2 c
u5 b
u4 b
u6 c
u2 c
u1 a
u2 a
u2 a
u3 a
u5 a
u5 a
u5 a
需求:
1.每個店鋪UV(訪客數)
2.每個店鋪訪問次數top3的訪客信息。輸出店鋪名、訪客id、訪問次數。
建表:
create table visit(user_id string,shop string) row format delimited fields terminated by '\t';
load data local inpath '/opt/soft/files/jd.txt' into table visit;
解題:
Q1:
方法1:
select shop,count(distinct user_id) UV from visit group by shop;
方法2:(優)
select shop,count(*) UV from (select shop,user_id from visit group by shop,user_id)t1 group by shop;
結果:
Q2:
select shop, user_id, ct from (select shop, user_id, ct, row_number() over(partition by shop order by ct desc) rk from (select shop, user_id, count(*) ct from visit group by shop,user_id)t1) t2 where rk<=3
結果: