如何获取配置单元中所有列的总和

时间:2017-02-20 11:23:43

标签: sql hive hiveql

是否可以获取hive表中所有列的总和。我的意思是采取任何一种方法来获取总和

表 col col_1 col_2 col_3

Ouptut sum(col),sum(col_1),sum(col_2)sum(col_3)

1 个答案:

答案 0 :(得分:3)

$('table tr td:nth-child(2):not([col-span]), table tr td:nth-child(2):not([row-span])')
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 |
+-----+---------+
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
;