如何计算hive中的累计工资

tquggr8v  于 2021-05-30  发布在  Hadoop
关注(0)|答案(2)|浏览(470)

由于配置单元只支持from cluase中的子查询,比如select*from(子查询),并且只支持eqjoin,所以我们如何从一个类似表的表中计算累计工资雇员有如下记录。

id     name       salary
e001  manish      10000
e002  amit         40000
e003  santosh     45000
e004  rohit       30000

所以输出应该如下所示

id       name           salary        cumsalary
e001     manish         10000          10000
e002     amit           40000          50000
e003     santosh        45000          95000
e004     rohit           30000         125000

我怎样才能在Hive里做到这一点

ctrmrzij

ctrmrzij1#

Hive支持 sum() 分析函数,因此您应该能够执行以下操作:

select t1.* ,
sum(salary) over (order by id) cumsalary
from table t1

有关配置单元分析函数的更多信息,请参见https://cwiki.apache.org/confluence/display/hive/languagemanual+windowingandanalytics

xiozqbni

xiozqbni2#

使用 Correlated Sub-Query 求累计和

create table #temp(id varchar(10),    name  varchar(50),     salary int)
 insert #temp values 
('e001',  'manish',      10000),
('e002',  'amit   ',      40000),
('e003',  'santosh ',    45000),
('e004',  'rohit  ',     30000)

SELECT *,
       (SELECT Sum(salary)
        FROM   #temp t2
        WHERE  t2.id <= t.id) As cumsalary
FROM   #temp t

相关问题