Reputation: 143
Is is possible to take sum of all columns in hive table. I mean any single way to take sum
Table col col_1 col_2 col_3
Ouptut sum(col), sum(col_1), sum(col_2) sum(col_3)
Upvotes: 2
Views: 3636
Reputation: 44921
create table mytable (i int,j int,k int);
insert into mytable values (1,2,3),(4,5,6),(7,8,9);
select pos+1 as col
,sum (val) as sum_col
from mytable t
lateral view posexplode(array(*)) pe
group by pos
;
+-----+---------+
| col | sum_col |
+-----+---------+
| 1 | 12 |
| 2 | 15 |
| 3 | 18 |
+-----+---------+
Or (So help me god)
select map_values
(
str_to_map
(
concat_ws
(
','
,sort_array
(
collect_list
(
concat_ws
(
':'
,lpad(cast(pos as string),10,'0')
,cast(sum_val as string)
)
)
)
)
)
) as sum_col_array
from (select pos
,sum (val) as sum_val
from mytable t
lateral view posexplode(array(*)) pe
group by pos
) t
;
+------------------+
| sum_col_array |
+------------------+
| ["12","15","18"] |
+------------------+
Upvotes: 3