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
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;
You can use the percentile function to compute the median. Try this:
select percentile(cast(age as BIGINT), 0.5) from table_name