Sql 如何获取配置单元中所有列的总和
可以获取配置单元表中所有列的总和。我是说任何一种求和的方法 桌子 第1列第2列第3列 乌普图 和(列)、和(列1)、和(列2)和(列3)Sql 如何获取配置单元中所有列的总和,sql,hive,hiveql,Sql,Hive,Hiveql,可以获取配置单元表中所有列的总和。我是说任何一种求和的方法 桌子 第1列第2列第3列 乌普图 和(列)、和(列1)、和(列2)和(列3) 或者(上帝保佑我) 问题不清楚我需要用一种方式把所有列的总和去掉,而不是每次都写总和(col),总和(col_1)写出来有什么问题?1000张桌子需要吗?你对结果做了什么? create table mytable (i int,j int,k int); insert into mytable values (1,2,3),(4,5,6),(7,8,9
或者(上帝保佑我)
问题不清楚我需要用一种方式把所有列的总和去掉,而不是每次都写总和(col),总和(col_1)写出来有什么问题?1000张桌子需要吗?你对结果做了什么?
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"] |
+------------------+