HIVE Query for Array Sum

房东的猫 提交于 2020-01-03 06:04:55

问题


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


来源:https://stackoverflow.com/questions/43649435/hive-query-for-array-sum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!