简体   繁体   English

查询以查找hiveQL中不包括前5%的行的平均值

[英]Query to find the average of the rows excluding the top 5% in hiveQL

select perecentile(time,0.95) from sometable;
 gives the 95th percentile.

I want average of all the rows whose time values are below this value. 我想要时间值低于此值的所有行的平均值。

In oracle it would be something like this:- 在甲骨文中将是这样的:

 select avg(time) from sometable
 where 
 time<(select percentile(time,0.95) from sometable);

But in hive it is not possible to use subqueries in the where clause.When i am using union all I am not able to isolate the tuple that i need to compare the other tuples with. 但是在蜂巢中,不可能在where子句中使用子查询。当我使用union时,我无法隔离需要与其他元组进行比较的元组。

you can do a Cartesian join with the result of the percentile and then filter all the smaller values. 您可以对百分比结果进行笛卡尔联接,然后过滤所有较小的值。

Something like this : 像这样的东西:

 select avg(time) from sometable a  
 join (select percentile(time,0.95) perc from sometable) b on (1=1)   
 where a.time < b.perc;

Its not the most efficient way but that's the first that comes in mind.. 它不是最有效的方法,但是这是第一个想到的方法。

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

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