问题
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