【问题标题】:creating nested array presto创建嵌套数组 presto
【发布时间】:2021-12-15 02:18:32
【问题描述】:

但我有这张桌子:

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}}         |
+----------------------------------------------------------+

【问题讨论】:

  • 实际输出是多少?

标签: sql amazon-athena presto


【解决方案1】:

您的结果不是嵌套数组,而是嵌套映射。我会说,除非这是一些更大查询的一部分,否则尝试将整个表映射到单行是很奇怪的,尤其是考虑到通常由 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}}

【讨论】:

  • 感谢sn-p
猜你喜欢
  • 2020-12-21
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 2011-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多