percentage calculation for each row in hive

百般思念 提交于 2019-12-23 05:43:17

问题


i have got a table in hive with the following schema (diference int,count_value int) The values are 5 2, 30 1, 90 1, 100 1

Now i want to find percentage of each count_value with sum of count_value. Something like count_value/sum(count_value) for each row. Can anybody please help. Thanks in advance


回答1:


With the new analytics and windowing functions introduced in Hive 0.11, you can do:

SELECT count_value / sum(count_value) over () as p from myTable

This avoids a join, plus easier to do the calculation if partitioned by another field. For example, if the source table had a key field and you wanted the calculation to use the sum from the rows with the same key, you could do:

SELECT count_value / sum(count_value) over (partition by key) as p from myTable



回答2:


How about using a subquery to calculate the total first, then joining the total to each row?

SELECT
    count_value / count_value_sum AS p
FROM
    myTable t
JOIN
    (SELECT SUM(count_value) AS count_value_sum FROM myTable) s

Hope that helps.



来源:https://stackoverflow.com/questions/18866853/percentage-calculation-for-each-row-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!