当前位置:   article > 正文

hive skills_hive 计算两个select结果的count比值

hive 计算两个select结果的count比值

1.with as 使用 + 求占比

  1. with sale as(
  2. select 'a' as department_id,'001' as item_id,100 as money union all
  3. select 'a' as department_id,'002' as item_id,200 as money union all
  4. select 'b' as department_id,'003' as item_id,300 as money union all
  5. select 'b' as department_id,'004' as item_id,400 as money)
  6. --求分组后占分组的占比
  7. select *,round(money/cast(sum(money)over(partition by department_id) as float),2) as p from sale;
  8. result
  9. sale.department_id sale.item_id sale.money p
  10. a 001 100 0.33
  11. a 002 200 0.67
  12. b 004 400 0.57
  13. b 003 300 0.43
  14. --求分组后占所有的占比
  15. select *,round(money/cast(sum(money)over() as float),2) as p from sale;
  16. result:
  17. sale.department_id sale.item_id sale.money p
  18. a 001 100 0.1
  19. b 003 300 0.3
  20. a 002 200 0.2
  21. b 004 400 0.4

2. hive将行结果转成列形式+求占比

  1. SELECT day,app_version,category,
  2. count(*) as `总用户数`,
  3. sum(`count`) as `总记录数`,
  4. floor(sum(`count`)/count(*)) as `人均记录数`,
  5. sum(case when isvalid=1 then 1 else 0 end) as `有效用户数`,
  6. sum(case when isvalid=0 then 1 else 0 end) as `无效用户数`,
  7. round(sum(case when isvalid=0 then 1 else 0 end)/count(*),4) as `无效用户数占比`
  8. from test
  9. WHERE app_version='3.5.0'
  10. GROUP BY day,app_version,category ORDER BY day,app_version,category

3.普通行结果求占比

  1. select day,app_version,category,connect_time,cnt,cnt/sum(cnt) over(partition by day,app_version,category) as p
  2. from (
  3. SELECT day,app_version,category,floor(connect_time/10)*10 as connect_time,
  4. count(*) as cnt
  5. from test
  6. WHERE isvalid=1 and keeptimes>24*3600*1000
  7. GROUP BY day,app_version,category,floor(connect_time/10)*10
  8. ) t
  9. group by day,app_version,category,connect_time,cnt
  10. ORDER BY day,app_version,category,connect_time,cnt

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/781988
推荐阅读
相关标签
  

闽ICP备14008679号