2017-02-20 29 views
1

是否可以获取配置单元表中的所有列的总和。我的意思是任何单一的方式,采取和如何获取配置单元中的所有列的总和

表 山坳COL_1 COL_2 col_3

Ouptut 总和(COL),和(COL_1),和(COL_2)和(col_3)

+0

问题不明确 –

+0

我需要总结过,而不是写每一次总和(COL),和单路的所有列(COL_1 ) – user2672739

+0

写这个问题有什么问题?你需要1000张桌子吗?你对结果做什么? –

回答

2
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 
; 

+------------------+ 
| sum_col_array | 
+------------------+ 
| ["12","15","18"] | 
+------------------+ 
相关问题