Hive分析函数之CUME_DIST、PERCENT_RANK学习

这两个分析函数比较不常用,或许用在一些特别要求的场合。
CUME_DIST 小于等于当前值的行数/分组内总行数。比如,统计小于等于当前薪水的人数,所占总人数的比例。
PERCENT_RANK 分组内当前行的RANK值-1/分组内总行数-1。

1、CUME_DIST使用示例
select 
pt_day,
appkey,
count(identifier),
cume_dist()over(order by count(identifier)) cd_all,
cume_dist()over(partition by appkey order by count(identifier)) cd_partition
from bi_all_access_log
where pt_day between '2017-11-01' and '2017-11-14'
group by pt_day,appkey;
2、PERCENT_RANK使用示例
select 
pt_day,
appkey,
count(identifier),
sum(1)over(partition by appkey) all_rows,
rank()over(order by count(identifier)) noparti_rk,
rank()over(partition by appkey order by count(identifier)) parti_rk,
percent_rank()over(order by count(identifier)) noparti_pr,
percent_rank()over(partition by appkey order by count(identifier)) parti_pr
from bi_all_access_log
where pt_day between '2017-11-01' and '2017-11-14'
group by pt_day,appkey;
说明sum(1)over(partition by appkey)可以统计出总行数。

你可能感兴趣的:(#,Hive,Sql)