How to calculate median in Hive

走远了吗. 提交于 2019-11-30 12:43:04

问题


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      4555

I want to calculate median of age column.

Below is my approach

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;

Appreciate any query suggestion


回答1:


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

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


来源:https://stackoverflow.com/questions/26863139/how-to-calculate-median-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!