2

我可以select在 Hive 中嵌套不同的条件吗?例如

如果我有以下两个 Hive 查询:

select percentile(x, 0.95)
from t1
where y = 1;

select percentile(x, 0.95)
from t1
where y = 2;

我可以在一个查询中选择上面的两个百分位数吗——比如(它不起作用):

select
  (select percentile(x, 0.95)
    from t1
    where y = 1),
  (select percentile(x, 0.95)
    from t1
    where y = 2)
from t1;
4

3 回答 3

3

您可以使用 来执行此操作UNION ALL,例如:

select * from
  (select percentile(x, 0.95)
    from t1
    where y = 1
   union all
   select percentile(x, 0.95)
    from t1
    where y = 2) x;
于 2013-01-25T01:35:08.027 回答
0

我认为您希望避免对表进行多次扫描,如果它非常大的话。

select percentile( if( y = 1 , x, 0 ), 0.95 ) as percentile_1
       percentile( if( y = 2 , x, 0 ), 0.95 ) as percentile_2
from t1;
于 2014-03-06T17:54:21.600 回答
0

也可以试试:

select percentile( case when y=1 then x else null end, 0.95) as p95_1
     , percentile( case when y=2 then x else null end, 0.95) as p95_2
from table;

percentile() 将忽略空值。

于 2015-06-25T20:24:49.200 回答