hive 的分析函式 視窗函式 | 排名函式 | 最大值 | 分層次 | lead && lag 統計活躍使用者 | cume_dist
1 preceding //前乙個
1 following //後乙個
current row //當前行
unbounded preceding //無上限
unbounded following //無下限
# 以行定義視窗界限113rankselect id, name, age , sum(age)over(order
by id rows between
current row and
2 following) from
user_par;
# 以值定義視窗界限
select id, name, age , sum(age)over(order
by age range between
current row and
10 following) from user_par;
select id, name, province, age , rank()over(partition by province order112dense_rankby age desc) from user_par;
select id, name, province, age , dense_rank()over(partition by province order123row_numberby age desc) from user_par;
select id, name, province, age , row_number()over(partition by province orderfirst_value()by age desc) from user_par;
select id, name, province, age , first_value()over(partition by province order按照三六九等進行平均分層by age desc) from user_par;
ntile()
select id, name, age , ntile(3)over(order將列向上提by age desc) from user_par;
select id, name, province, age , lead(age)over(partition by province order將列向下沉by age asc) from user_par;
select id, name, province, age , lag(age)over(partition by province order1. 準備資料by age asc) from user_par;
2. 建表
create3. 載入資料table active(id string, month
int)
row format delimited
fields terminated by'
\t';
load data local inpath '4. 統計連續兩月活躍使用者/home/centos/files/active.txt
'into
table active;
select id from (select id, month, lead(month)over(partition by id order指定值佔總數的百分比bymonth
desc) as month2 from active)a where
month
=month2+
1;
分析函式hive計算均值 Hive 分析函式
應用場景 1 用於分割槽排序 2 top n 3 層次查詢 常用分析函式 分析函式 描述 rank 返回資料項在分割槽中的排名。排名值序列可能會有間隔 dense rank 返回資料項在分割槽中的排名。排名值序列是連續的,沒有間隔 percent rank 計算當前行的百分比排名 x 1 視窗分割槽...
hive分析函式
遇到以下問題如何解決?查詢客戶各個日期的歷史累積購買金額 查詢每個客戶第一次 首購 或前n次購買記錄 查詢每個客戶最後一次購買記錄 某活動推廣後,每天的累積資料 查詢客戶每天的歷史累積購買金額 hive 語法 select userid user id,pay datekey pay datekey...
Hive的分析函式
記錄下hive的常用函式 hive的常用函式和平常我們使用的關係型資料庫基本都差不多,這裡只是記錄一下,方便後面回顧。準備測試資料.hadoop hadoop apache hive 0.13.1 bin cat emp.txt 7369 smith clerk 7902 1980 12 17 80...