如何计算Hive的中位数

时间:2014-11-11 10:51:56

标签: hive hiveql

我有一个蜂巢表,

name    age     sal
A       45      1222
B       50      4555
c       44      8888
D       78      1222
E       12      7888
F       23      4555

我想计算年龄栏的中位数。

以下是我的方法

select min(age) as HMIN,max(age) as HMAX,count(age) as HCount,
IF(count(age)%2=0,'even','Odd') as PCOUNT 
from v_act_subjects_bh;

感谢任何查询建议

2 个答案:

答案 0 :(得分:65)

您可以使用百分位函数来计算中位数。试试这个:

select percentile(cast(age as BIGINT), 0.5) from table_name

答案 1 :(得分:-1)

double median = 0;
double term = 0;
double term1 = 0;
if (size % 2 == 1)
{
    term = (size + 1 - 1) / 2;
    median = term;
}
else if (size % 2 == 0)

{
    
    term1 = (size - 1) / 2;
    term1 = term1 + ((size - 1) / 2) + 1;
    term1 = term1 / 2;
    median = term1;
}
cout << "Median of array: " << median << endl;