Q1和Q2在不同方面是相同的问题。 如果数据存储为元组(键,值),那么任何SQL都能得到相同的结果?
(1,3)(2,5)(4,7)
(1,3)(2,5)(3,4)
(2,3)(7,5)(10,4)
Q1:sumMap可以将Array更改为Tuple,但是如何将Tuple更改为Array?
select sumMap(a, b) from (
select array(1,2,4) as a, array(3,5,7) as b
union all
select array(1,2,3) as a, array(3,5,4) as b
union all
select array(2,7,10) as a, array(3,5,4) as b);
│ ([1,2,3,4,7,10],[6,13,4,7,5,4]) │
错误SQL:
select sumMap(a, b).[0], sumMap(a, b).[1] from tbl
[1,2,3,4,7,10] [6,13,4,7,5,4]
第二季度:如何像sumMap这样按键求和数组?
select array(1,2,4) as a, array(3,5,7) as b
union all
select array(1,2,3) as a, array(3,5,4) as b
union all
select array(2,7,10) as a, array(3,5,4) as b
│ [1,2,4] │ [3,5,7] │
│ [2,7,10]│ [3,5,4] │
│ [1,2,3] │ [3,5,4] │
错误SQL:
select sumBykey(a, a), sumBykey(b, a).key2 from tbl
[1,2,3,4,7,10] [6,13,4,7,5,4]
答案 0 :(得分:0)
SELECT
sumMap(a, b) AS summap,
summap.1 AS a1,
summap.2 AS a2
FROM
(
SELECT
[1, 2, 4] AS a,
[3, 5, 7] AS b
UNION ALL
SELECT
[1, 2, 3] AS a,
[3, 5, 4] AS b
UNION ALL
SELECT
[2, 7, 10] AS a,
[3, 5, 4] AS b
)
┌─summap──────────────────────────┬─a1─────────────┬─a2─────────────┐
│ ([1,2,3,4,7,10],[6,13,4,7,5,4]) │ [1,2,3,4,7,10] │ [6,13,4,7,5,4] │
└─────────────────────────────────┴────────────────┴────────────────┘