简体   繁体   中英

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 .

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);

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM