HQL如何使用分位数函数

在做一些酒店产量分析时,用到统计学中常用的分位数函数,所以就学习了一下分位数函数在HIVE中的应用。

HIVE中有两个关于分为数的函数:percentilepercentile_approx。

使用方式:

percentile:percentile(col, p) col是要计算的列(值必须为int类型),p的取值为0-1,若为0.2,那么就是2分位数,依次类推。

percentile_approx:percentile_approx(col, p)。列为数值类型都可以。

percentile_approx还有一种形式percentile_approx(col, p,B),参数B控制内存消耗的近似精度,B越大,结果的精度越高。默认值为10000。当col字段中的distinct值的个数小于B时,结果就为准确的百分位数。

如果需要多个分位数,可以一次性取出来,案例如下:

去每天的UV的2分位数、4分位数、6分位数、8分位数:

select d, 
       percentile_approx(uv, array(0.2,0.4,0.6,0.8), 9999) as uv --2%分位数作为最小值       
  from dw_htldatadb.tmp_adm_htl_ebkbl_everyday_ceshi
group by d

结果如下:

猜你喜欢

转载自blog.csdn.net/Jarry_cm/article/details/82185576