计算同一级别下分支机构的金额百分比

时间:2019-03-08 22:49:04

标签: hive hql impala

我正在尝试计算同一级别下每个分支的百分比贡献。 例如,我们在下面的输入表Amount和Branch J60和J90在层次结构表的同一L1 X900下,则贡献百分比为sum(J60)/ sum(J60 + J90)。 MQ0是X600下唯一的分支,因此百分比为200/200。

类似地,L0级别百分比J60,J90,MQ0在同一级别0分支M1200下,那么百分比将类似于J60 / J60 + J90 + MQ0

如何找到这个百分比贡献。

金额:

+--------+---------+-------+
| amount | branch  | Level |
+--------+---------+-------+
| 1000   | J60     |  L2   |
+--------+---------+-------+
| 200    | MQ0     |  L2   |
+--------+---------+-------+
| 1500   | X600    |  L1   |
+--------+---------+-------+
| 100    | J90     |  L2   |
+--------+---------+-------+
| 1200   | X900    |  L1   |
+--------+---------+-------+
| 2000   | M1200   |  L0   |
+--------+---------+-------+

层次结构表:它具有分支代码的级别映射。

+---------+----------+-----------+
| level0  | level1   | level2    |
+---------+----------+-----------+
| M1200   | X900     | J60       |
+---------+----------+-----------+
| M1200   | X600     | MQ0       |
+---------+----------+-----------+
| M1200   | X900     | J90       |
+---------+----------+-----------+

预期的输出表:

+--------+---------+-------+--------------+--------------+
| amount | branch  | Level | L1 Percent   | L0 Percent   |
+--------+---------+-------+--------------+--------------+
| 1000   | J60     |  L2   | 1000/1100    | 1000/1300    |
+--------+---------+-------+--------------+--------------+
| 200    | MQ0     |  L2   | 200/200      | 200/1300     |
+--------+---------+-------+--------------+--------------+
| 1500   | X600    |  L1   |    NA        | 1500/1700    |
+--------+---------+-------+--------------+--------------+
| 100    | J90     |  L2   | 100/1100     | 100/1300     |
+--------+---------+-------+--------------+--------------+
| 1200   | X900    |  L1   |    NA        | 1200/1700    |
+--------+---------+-------+--------------+--------------+
| 2000   | M1200   |  L0   |    NA        | NA           |
+--------+---------+-------+--------------+--------------+

0 个答案:

没有答案