嵌套在Hive中选择

时间:2013-01-24 23:07:18

标签: hive

我可以在Hive中嵌套具有不同条件的select吗? e.g。

如果我有以下两个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;

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;

答案 2 :(得分: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;

百分位数()将忽略空值。