hiveSql

第十二單元 常用函數(shù)轻猖、窗口函數(shù)(開(kāi)窗函數(shù))

1爆班、常用函數(shù)

1.1 字符串
--字符串拼接函數(shù) concat
select concat('abc','def');
select concat_ws('-','abc','def');

--求字符串長(zhǎng)度 length
select length('jsdfijsdkfjkdsfjkdf');


1.2 日期
--日期函數(shù) to_date
select to_date('2019-09-11 16:55:11');

--把字符串轉(zhuǎn)換成unix時(shí)間戳
select unix_timestamp('2019-09-11 11:55:11','yyyy-MM-dd HH:mm:ss');

--得到現(xiàn)在的時(shí)間戳
select unix_timestamp();

--把unix時(shí)間戳轉(zhuǎn)換成字符串
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss');

1.3 數(shù)學(xué)運(yùn)算函數(shù)
--四舍五入
select round(5.4)
--四舍五入保留2位小數(shù)
select round(5.1345,2)
--向上取整
select ceil(5.3)
--向下取整
select floor(5.3)
--取絕對(duì)值
select abs(-5.2)
--取最大值
select greatest(3,4,5,6,7)
select max
--取最小值
select least(3,4,5,6,7)
select min
--隨機(jī)數(shù)
rand();

2、窗口函數(shù)(開(kāi)窗函數(shù))

在開(kāi)窗函數(shù)出現(xiàn)之前存在著很多用 SQL 語(yǔ)句很難解決的問(wèn)題,很多都要通過(guò)復(fù)雜的相關(guān)子查詢(xún)或者存儲(chǔ)過(guò)程來(lái)完成。為了解決這些問(wèn)題仓技,在2003年ISO SQL標(biāo)準(zhǔn)加入了開(kāi)窗函數(shù),開(kāi)窗函數(shù)的使用使得這些經(jīng)典的難題可以被輕松的解決俗他。目前在 MSSQLServer脖捻、Oracle、DB2 等主流數(shù)據(jù)庫(kù)中都提供了對(duì)開(kāi)窗函數(shù)的支持兆衅,不過(guò)非常遺憾的是 MYSQL 暫時(shí)還未對(duì)開(kāi)窗函數(shù)給予支持地沮。

與聚合函數(shù)一樣嗜浮,開(kāi)窗函數(shù)也是對(duì)行集組進(jìn)行聚合計(jì)算,但是它不像普通聚合函數(shù)那樣每組只返回一個(gè)值摩疑,開(kāi)窗函數(shù)可以為每組返回多個(gè)值危融,因?yàn)殚_(kāi)窗函數(shù)所執(zhí)行聚合計(jì)算的行集組是窗口。

2.1 窗口---排序

1,18,a,male
2,19,b,male
3,19,c,male
4,22,d,female
5,22,e,female
6,16,f,female
7,30,g,male
8,26,h,female

建表:

create table t_student(id int,age int,name string,sex string)
row format delimited fields terminated by ',';

導(dǎo)入數(shù)據(jù):

load data local inpath '/root/student.dat' into table t_student;

row_number() over:

-- select * from t_student group by sex;能否執(zhí)行未荒?专挪?
-- select sex,max(age) from t_student group by sex; 能否執(zhí)行及志?片排?
-- select id,age,name,sex,max(age) from t_student group by sex; 能否執(zhí)行?速侈?
-- 常規(guī)group by 只能查聚合的字段或使用聚合函數(shù)率寡,若使聚合后返回多個(gè)值則需要更復(fù)雜的操作

select 
id,age,name,sex,row_number() over(partition by sex order by age desc) rk
from t_student;

+-----+------+-------+---------+-----+--+
| id  | age  | name  |   sex   | rk  |
+-----+------+-------+---------+-----+--+
| 8   | 26   | h     | female  | 1   |
| 5   | 22   | e     | female  | 2   |
| 4   | 22   | d     | female  | 3   |
| 6   | 16   | f     | female  | 4   |
| 7   | 30   | g     | male    | 1   |
| 3   | 19   | c     | male    | 2   |
| 2   | 19   | b     | male    | 3   |
| 1   | 18   | a     | male    | 4   |
+-----+------+-------+---------+-----+--+

分組求Top1、TopN:

select tmp.* from 
(select 
id,age,name,sex,row_number() over(partition by sex order by age desc) rk
from t_student) tmp
where tmp.rk = 1;

select tmp.* from 
(select 
id,age,name,sex,row_number() over(partition by sex order by age desc) rk
from t_student) tmp
where tmp.rk <=3;

rank() over倚搬、dense_rank() over冶共、ntile(n) over

select 
id,age,name,sex,rank() over(partition by sex order by age desc) rk
from t_student;

+-----+------+-------+---------+-----+--+
| id  | age  | name  |   sex   | rk  |
+-----+------+-------+---------+-----+--+
| 8   | 26   | h     | female  | 1   |
| 5   | 22   | e     | female  | 2   |
| 4   | 22   | d     | female  | 2   |
| 6   | 16   | f     | female  | 4   |
| 7   | 30   | g     | male    | 1   |
| 3   | 19   | c     | male    | 2   |
| 2   | 19   | b     | male    | 2   |
| 1   | 18   | a     | male    | 4   |
+-----+------+-------+---------+-----+--+

select 
id,age,name,sex,dense_rank() over(partition by sex order by age desc) rk
from t_student;

+-----+------+-------+---------+-----+--+
| id  | age  | name  |   sex   | rk  |
+-----+------+-------+---------+-----+--+
| 8   | 26   | h     | female  | 1   |
| 5   | 22   | e     | female  | 2   |
| 4   | 22   | d     | female  | 2   |
| 6   | 16   | f     | female  | 3   |
| 7   | 30   | g     | male    | 1   |
| 3   | 19   | c     | male    | 2   |
| 2   | 19   | b     | male    | 2   |
| 1   | 18   | a     | male    | 3   |
+-----+------+-------+---------+-----+--+

select
id,age,name,sex,ntile(3) over(partition by sex order by age desc) rk
from
t_student;

+-----+------+-------+---------+-----+--+
| id  | age  | name  |   sex   | rk  |
+-----+------+-------+---------+-----+--+
| 8   | 26   | h     | female  | 1   |
| 5   | 22   | e     | female  | 1   |
| 4   | 22   | d     | female  | 2   |
| 6   | 16   | f     | female  | 2   |
| 7   | 30   | g     | male    | 1   |
| 3   | 19   | c     | male    | 1   |
| 2   | 19   | b     | male    | 2   |
| 1   | 18   | a     | male    | 2   |
+-----+------+-------+---------+-----+--+
2.2 窗口---求和

A,2015-01,5
A,2015-01,15
B,2015-01,5
A,2015-01,8
B,2015-01,25
A,2015-01,5
C,2015-01,10
C,2015-01,20
A,2015-02,4
A,2015-02,6
C,2015-02,30
C,2015-02,10
B,2015-02,10
B,2015-02,5
A,2015-03,14
A,2015-03,6
B,2015-03,20
B,2015-03,25
C,2015-03,10
C,2015-03,20

建表:

create table t_saller(name string,month string,amount int)
row format delimited fields terminated by ',';

導(dǎo)入數(shù)據(jù):

load data local inpath '/root/saller.dat' overwrite into table t_saller;

求每個(gè)銷(xiāo)售員每個(gè)月的銷(xiāo)售額和到當(dāng)月為止的累計(jì)銷(xiāo)售額:

-- 求每個(gè)銷(xiāo)售員每月銷(xiāo)售額,先按照銷(xiāo)售員分組,再按月份分組
select name,month,sum(amount) samount from t_saller group by name,month;

+-------+----------+----------+--+
| name  |  month   | samount  |
+-------+----------+----------+--+
| A     | 2015-01  | 33       |
| A     | 2015-02  | 10       |
| A     | 2015-03  | 20       |
| B     | 2015-01  | 30       |
| B     | 2015-02  | 15       |
| B     | 2015-03  | 45       |
| C     | 2015-01  | 30       |
| C     | 2015-02  | 40       |
| C     | 2015-03  | 30       |
+-------+----------+----------+--+


-- 為避免過(guò)多子查詢(xún)每界,課提前創(chuàng)建 月銷(xiāo)售額表:
create table t_accumulate
as
select name,month,sum(amount) samount from t_saller group by name,month;

-- 累加 從最前面一行到當(dāng)前行 between unbounded preceding and current row
-- preceding 往前
-- unbounded 起點(diǎn)
-- current row 當(dāng)前行
-- unbounded preceding 最前一行
-- unbounded following  最后一行
-- following 窗口長(zhǎng)度

select 
name,month,samount,
sum(samount) over(partition by name order by month rows between unbounded preceding and current row) accumlateAmount
from
t_accumulate;
+-------+----------+----------+------------------+--+
| name  |  month   | samount  | accumlateamount  |
+-------+----------+----------+------------------+--+
| A     | 2015-01  | 33       | 33               |
| A     | 2015-02  | 10       | 43               |
| A     | 2015-03  | 20       | 63               |
| B     | 2015-01  | 30       | 30               |
| B     | 2015-02  | 15       | 45               |
| B     | 2015-03  | 45       | 90               |
| C     | 2015-01  | 30       | 30               |
| C     | 2015-02  | 40       | 70               |
| C     | 2015-03  | 30       | 100              |
+-------+----------+----------+------------------+--+

--其他 between 2 preceding  and 1 following
select 
name,month,samount,sum(samount) over(partition by name order by month rows between 2 preceding  and 1 following ) accumlateAmount
from
t_accumulate;

2.3 窗口---其他
min()  over()    ,max() over() ,   avg() over()

3捅僵、explode()

名詞解釋?zhuān)篹xplode 爆炸

1,zhangsan,化學(xué):物理:數(shù)學(xué):語(yǔ)文
2,lisi,化學(xué):數(shù)學(xué):生物:生理:衛(wèi)生
3,wangwu,化學(xué):語(yǔ)文:英語(yǔ):體育:生物

建表:

create table t_stu_subject(id int,name string,subjects array<string>)
row format delimited fields terminated by ','
collection items terminated by ':';

導(dǎo)入數(shù)據(jù):

load data local inpath '/root/stu_subject.dat'  into table t_stu_subject;

需求:從學(xué)生學(xué)習(xí)課程分析這個(gè)學(xué)校都有哪些課程

-- 炸科目:explode(subjects) 
select explode(subjects) from t_stu_subject;

-- 去重
select distinct tmp.subs from (select explode(subjects) subs from t_stu_subject) tmp;

lateral view 連接函數(shù)

select  id,name,sub from 
t_stu_subject 
lateral view explode(subjects) tmp as sub;

+-----+-----------+------+--+
| id  |   name    | sub  |
+-----+-----------+------+--+
| 1   | zhangsan  | 化學(xué)   |
| 1   | zhangsan  | 物理   |
| 1   | zhangsan  | 數(shù)學(xué)   |
| 1   | zhangsan  | 語(yǔ)文   |
| 2   | lisi      | 化學(xué)   |
| 2   | lisi      | 數(shù)學(xué)   |
| 2   | lisi      | 生物   |
| 2   | lisi      | 生理   |
| 2   | lisi      | 衛(wèi)生   |
| 3   | wangwu    | 化學(xué)   |
| 3   | wangwu    | 語(yǔ)文   |
| 3   | wangwu    | 英語(yǔ)   |
| 3   | wangwu    | 體育   |
| 3   | wangwu    | 生物   |
+-----+-----------+------+--+

-- 單詞統(tǒng)計(jì) wordcount
-- split()
select
tmp.word,count(1) cnts from 
(select  explode(split(line,' ')) word from words) tmp
group by tmp.word order by cnts desc;

-- 炸map
select  id,name,key,value from  t_people
lateral view explode(family) tmp as key,value;

第十三單元 Hive 自定義函數(shù),Hive API眨层、Hive腳本

1月11號(hào)

第十四單元 其他

1月12號(hào)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末庙楚,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子趴樱,更是在濱河造成了極大的恐慌馒闷,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叁征,死亡現(xiàn)場(chǎng)離奇詭異纳账,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)捺疼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén)疏虫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人啤呼,你說(shuō)我怎么就攤上這事卧秘。” “怎么了媳友?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵斯议,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我醇锚,道長(zhǎng)洒嗤,這世上最難降的妖魔是什么糊余? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任研儒,我火速辦了婚禮,結(jié)果婚禮上看靠,老公的妹妹穿的比我還像新娘。我一直安慰自己液肌,他們只是感情好挟炬,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著嗦哆,像睡著了一般谤祖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上老速,一...
    開(kāi)封第一講書(shū)人閱讀 49,772評(píng)論 1 290
  • 那天粥喜,我揣著相機(jī)與錄音,去河邊找鬼橘券。 笑死额湘,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的旁舰。 我是一名探鬼主播锋华,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼箭窜!你這毒婦竟也來(lái)了毯焕?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤绽快,失蹤者是張志新(化名)和其女友劉穎芥丧,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體坊罢,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡续担,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了活孩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片物遇。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖憾儒,靈堂內(nèi)的尸體忽然破棺而出询兴,到底是詐尸還是另有隱情,我是刑警寧澤起趾,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布诗舰,位于F島的核電站,受9級(jí)特大地震影響训裆,放射性物質(zhì)發(fā)生泄漏眶根。R本人自食惡果不足惜蜀铲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望属百。 院中可真熱鬧记劝,春花似錦、人聲如沸族扰。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)渔呵。三九已至怒竿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間厘肮,已是汗流浹背愧口。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留类茂,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓托嚣,卻偏偏與公主長(zhǎng)得像巩检,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子示启,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348