Hive ql如何使用多个COUNT函数进行查询,并使用它们进行除法

Car*_*Lin 4 hadoop hive

在这里我的问题是:我有一个表,其中包含一些记录,如(名称,日期,类型).假设我有三个类型a,b和c.现在我想将每个类型计为type_count但有一些限制并使用count(a)/ count(b)进行除法以得到百分比结果,并且a和a中的限制是不同,我该怎么处理?谢谢!我的代码看起来像这样:

   SELECT name, count(a), count(a)/count(b)
   from table
   where ...
Run Code Online (Sandbox Code Playgroud)

是否可以在select中制作一些子查询?看起来像这样

select name, count(a), count(a)/ (select count(b) from table where restriction_for_b)
from table
where retriction_for_a
Run Code Online (Sandbox Code Playgroud)

Joe*_*e K 10

如果我正确理解您的问题,您可以用计数替换计数sum(if(condition, 1, 0)).像这样的东西:

select
  name,
  sum(if(condition_for_a, 1, 0)),
  sum(if(condition_for_a, 1, 0)) / sum(if(condition_for_b, 1, 0))
from table
where ...
Run Code Online (Sandbox Code Playgroud)