简体   繁体   中英

Calculate conversion rate with SQL in Hive

I`m trying to calculate CR(Conversion Rate) of column name 'action_type'.

The column 'action_type' is allocated to string values as follows...

  • 1 : sale
  • 2 : click

So, I think the conversion rate is '(SUM(sale) / SUM(click)) * 100".

The values '1'(sale) and '2'(click) are in the same column 'action_type'.

How do I write a sql query in hive?

Thank you!

You would do this using case and aggregation:

select (sum(case when action_type = 'sale' then 100.0 else 0 end) /
        sum(case when action_type = 'click' then 1.0 end)
       ) as conversion_rate
from t;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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