创建嵌套数组 presto

creating nested array presto

但是我有这个table:

with cte (customer_id, product, sell) as (
  values 
  (1, 'a', 100), 
  (1, 'b', 150),
  (2, 'a', 90),
  (2, 'b', 110)
)

select * from cte

我想要如下结果:

+----------------------------------------------------------+
| result                                                   |
+----------------------------------------------------------+
| {1: {"a": 100, "b": 150}, 2: {"a":90, "b": 110}}         |
+----------------------------------------------------------+

您的结果不是嵌套数组而是嵌套映射。我会说,除非这是一些更大查询的一部分,否则尝试将整个 table 映射到单行是很奇怪的,特别是考虑到通常由 Athena 处理的数据大小,但对于此测试数据,您可以使用 map_agg 和嵌套分组:

with cte (customer_id, product, sell) as (
    values (1, 'a', 100),
        (1, 'b', 150),
        (2, 'a', 90),
        (2, 'b', 110)
)

select map_agg(customer_id, m) as result                                                   
from (
        select customer_id, map_agg(product, sell) m
        from cte
        group by customer_id
    )
group by true -- fake grouping 

输出:

result
{1={a=100, b=150}, 2={a=90, b=110}}