row number
作用 : 返回這個Partition下的當(dāng)前Row號败晴,根據(jù)排序字段生成,無重復(fù)鼓黔。
語法 :row_number() over (partition by 字段a order by 計算項b desc ) rank
select seasonid, episodeid ,
row_number() over(partition by seasonid order by episodeid asc) as rank
from tutor.dw_season_live_information a
where dt = date_sub(current_date,1) limit 100
內(nèi)置rank分析函數(shù)區(qū)別 :
- row_number:不管排名是否有相同的佑笋,都按照順序1,2丁寄,3…..n
- rank:排名相同的名次一樣,同一排名有幾個泊愧,后面排名就會跳過幾次
- dense_rank:排名相同的名次一樣伊磺,且后面名次不跳躍
with cube & grouping sets & rollup
主要用于不能用簡單group by語句處理需求的情況
grouping sets : 其后面跟括號里指定了零個或多個分組變量的聚合,然后產(chǎn)生這個維度下的聚合結(jié)果删咱,再將每個結(jié)果UNION到一起屑埋,其實也就是相當(dāng)于
select a,sum(xx) form table1 … group by a
union
select b,sum(xx) form table1 … group by b
union
select sum(xx) form table1 … group by a,b
union
...
這個意思
用法:
select ga, gb, sum(val) from table
group by ga, gb
GROUPING SETS ((ga), (gb), ());
with cube : 對變量進行有/無的組合,如果有三個對象腋腮,就會產(chǎn)生2^3 = 8種聚合情況
用法 :
eg :
cube (a,b,c)
equal
grouping sets( (a,b,c), (a,b), (a,c), (a),(b,c), (b), (c), () )
rollup : 是cube的子集雀彼,以最左側(cè)的維度為主,從該維度進行層級聚合
用法 :
eg :
rollup(a,b,c)
equal
grouping sets( (a,b,c),(a,b),(a),() )
regexp_extract
字符串正則表達式解析函數(shù)
用法 :
eg :
select regexp_extract( 'yfd-mkt-0207juzhanYY05-grp-123', 'yfd-mkt-([^\\-]+)-.*', 1) as keyfrom_type; — res : 0207juzhanYY05
0 : 表示把整個正則表達式對應(yīng)的結(jié)果全部返回
1 :表示返回表達式中第一個()對應(yīng)的結(jié)果
依次類推...
相關(guān)函數(shù) : regexp_replace()
get_json_object
作用 : 從復(fù)雜json對象中提取元素
‘$’表示對Root對象
eg : get_json_object(t.info,’$.xx')
時間處理 :
一種思路 : 可以把時間轉(zhuǎn)化為一個整數(shù)即寡,經(jīng)過處理之后徊哑,再轉(zhuǎn)化為日期格式。
常見hive時間轉(zhuǎn)換函數(shù)
- from_unixtime : 日期函數(shù)UNIX時間戳轉(zhuǎn)日期函數(shù): from_unixtime語法: from_unixtime(bigint unixtime[, string format]),可以是識別到月
- unix_timestamp(string date, string pattern) : 指定格式日期轉(zhuǎn)UNIX時間戳函數(shù)聪富,可以識別到月
hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss');
1323234063
hive> select unix_timestamp('2011-12-07 13:05','yyyy-MM-dd HH:mm');
1323234300
hive> select unix_timestamp('2011-12','yyyy-MM');
1322668800
- date_format :
hive> select date_format('2015-04-08', 'y');
2015
hive> select date_format('2015-04-08', 'yyyy');
2015
hive> select date_format('2015-04-08', 'yyyy-MM');
2015-04
hive> select date_format('2015-04-08 10:10:01', 'yyyy-MM');
2015-04
hive> select date_format('2015-04-08', 'yyyy-MM-dd');
2015-04-08
- to_date
hive> select to_date('2011-12-08 10:03:01');
2011-12-08
hive> select to_date('2011-12-08');
2011-12-08
hive> select to_date('2011-12');
NULL
- date_sub | date_add
窗口函數(shù) :
待更新...
lag, first_value, last_value , lead