2017-02-20 64 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"] | 
+------------------+ 
相關問題