HIVE Query for Array Sum

2019-06-12 12:47发布

问题:

I have a query as below. Select split(Salary, '\|') as salaryEmp from tableA and it works fine and gives me a an array string as ["1089","1078"].

I would want to add the values of this array string. I am not able to type cast it to integer and sum them. Can a suitable way be suggested for this.

回答1:

select  sum(e.col) as sum_Salary
from    salaryEmp lateral view explode (split(Salary,'\\|')) e

+------------+
| sum_salary |
+------------+
|       2167 |
+------------+


回答2:

Use explode() + lateral view:

select sum(cast(salary as int)) sum_salry from
 (
 select split('1089|1078', '\\|') SalaryArray 
 ) s lateral view explode (SalaryArray) a as Salary;

OK
2167


标签: hive hiveql