【问题标题】:extract array of arrays in presto在 presto 中提取数组数组
【发布时间】:2021-05-01 23:30:45
【问题描述】:

我在 Athena (presto) 中有一个表,其中只有一个名为 individuals 的列,这是 then 列的类型:

array(row(individual_id varchar, ids array(row(type varchar, value varchar, score integer))))

我想从ids 中提取value 并将它们作为新数组返回。举个例子:

[{individual_id=B1Q, ids=[{type=H, value=efd3, score=1}, {type=K, value=NpS, score=1}]}, {individual_id=D6n, ids=[{type=A, value=178, score=6}, {type=K, value=NuHV, score=8}]}]

我想回来

ids
[efd3, NpS, 178, NuHV]

我尝试了多种解决方案,例如

select * from "test"
CROSS JOIN UNNEST(individuals.ids.value) AS t(i)

但总是返回

 Expression individuals is not of type ROW

【问题讨论】:

    标签: struct presto amazon-athena unnest trino


    【解决方案1】:
    select
      array_agg(ids.value)
    from test
    cross join unnest(test.individuals) t(ind)
    cross join unnest(ind.ids) t(ids)
    

    结果:

    [efd3, NpS, 178, NuHV]
    

    这会将所有 id 值作为一行返回,这可能是也可能不是你想要的

    如果你想通过 individual_id 返回一个单独的值数组:

    select
      ind.individual_id,
      array_agg(ids.value)
    from test
    cross join unnest(test.individuals) t(ind)
    cross join unnest(ind.ids) t(ids)
    group by
      ind.individual_id
    

    【讨论】:

      猜你喜欢
      • 2019-06-24
      • 2019-03-07
      • 2020-09-25
      • 2021-05-06
      • 2020-04-18
      • 2020-12-24
      • 2019-12-18
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多