Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Hive ql do query with multiple COUNT function and make a divides methon with them

Tags:

hadoop

hive

Here my question: I have a table with some records like (name, date, type). Suppose I have three type a, b and c.Now I want to count each type as type_count with some restriction and make a division with count(a)/count(b) to make a percent result, and the restriction in a and are different, how can I deal with this? thanks! my code look like this:

   SELECT name, count(a), count(a)/count(b)
   from table
   where ...

Is it possible to make some subquery in select? look like this

select name, count(a), count(a)/ (select count(b) from table where restriction_for_b)
from table
where retriction_for_a
like image 234
Carlos Lin Avatar asked Dec 15 '22 20:12

Carlos Lin


1 Answers

If I understand your question correctly, you can replace the counts with sum(if(condition, 1, 0)). Something like this:

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 ...
like image 141
Joe K Avatar answered May 14 '23 06:05

Joe K