How to calculate median in Hive

后端 未结 2 872
独厮守ぢ
独厮守ぢ 2020-12-25 11:33

I have a hive table,

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


        
相关标签:
2条回答
  • 2020-12-25 11:55
    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;
    
    0 讨论(0)
  • 2020-12-25 11:59

    You can use the percentile function to compute the median. Try this:

    select percentile(cast(age as BIGINT), 0.5) from table_name
    
    0 讨论(0)
提交回复
热议问题