Adi
Adi

Reputation: 4230

Hive: How to select the middle element order by some column

What is the hive query to select the middle element ordered by some column.

Example:

Name      age
A          10
B          20
C          30

Output: B 20.

Upvotes: 1

Views: 1287

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

The middle element is the median of the column. There are several ways to do this. A reliable way is:

select avg(age)
from (select t.*,
             row_number() over (order by age) as seqnum,
             count(*) over () as cnt
      from t
     ) t
where seqnum * 2 in (cnt, cnt + 1, cnt + 2);

This works for both even and odd numbers of rows. It does assume that "age" is numeric (so avg() will work).

Upvotes: 1

Gurwinder Singh
Gurwinder Singh

Reputation: 39477

You can find the middle row using analytic functions row_number() and count() like so:

select name, age
from (
select
    name,
    age,
    row_number() over (order by your_order_by_list) r,
    count(*) over () c
from
    your_table) t
where r = cast((c + 1) / 2 as int);

Upvotes: 2

Related Questions