简体   繁体   English

蜂巢:如何通过某些列选择中间元素顺序

[英]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 . 输出: B 20

You can find the middle row using analytic functions row_number() and count() like so: 您可以使用解析函数row_number()和count()找到中间行,如下所示:

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). 它确实假定“年龄”是数字(因此avg()将起作用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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