Hive 10 Hive 的分析函式

2022-07-24 15:27:30 字數 2322 閱讀 9102

hive 的分析函式 視窗函式  | 排名函式 | 最大值 | 分層次 | lead && lag 統計活躍使用者 | cume_dist

1 preceding  //前乙個

1 following  //後乙個

current row  //當前行

unbounded preceding  //無上限

unbounded following  //無下限

# 以行定義視窗界限

select 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;

113rank

select id, name, province, age , rank()over(partition by province order

by age desc) from user_par;

112dense_rank

select id, name, province, age , dense_rank()over(partition by province order

by age desc) from user_par;

123row_number

select id, name, province, age , row_number()over(partition by province order

by age desc) from user_par;

first_value()

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 order

by age asc) from user_par;

1. 準備資料

2. 建表

create

table active(id string, month

int)

row format delimited

fields terminated by'

\t';

3. 載入資料

load data local inpath '

/home/centos/files/active.txt

'into

table active;

4. 統計連續兩月活躍使用者

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...