配置单元中的每一行的百分比计算 [英] percentage calculation for each row in hive
问题描述
我有一个配置单元的表格,其格式如下
(diference int,count_value int)
这些值是
5 2,
30 1,
90 1,
100 1
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
现在我想查找每个count_value与count_value之和的百分比。像count_value / sum(count_value)为每一行。任何人都可以请帮忙。提前致谢
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
推荐答案
通过Hive 0.11中引入的新分析和窗口功能,您可以执行:
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
这篇关于配置单元中的每一行的百分比计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!