在 Presto 中如何检索具有唯一元素的数组

In Presto how to retrieve an array with unique elements

我有一个问题

SELECT id, id_arr from RESULTS

它 returns 这样的结果:


    123             [742] 
    123             [332,180]
    123             [742,180,039] 
    123             [244,302, 742] 
    123             [412] 

如何在 Presto 中构造一个查询来生成 ID 和仅包含唯一元素的组合数组?我知道 ARRAY_UNION 函数,但在使用它时遇到问题。

您也可以尝试聚合数组并使用以下方法查找其中的不同元素:

SELECT
    id, 
    set_union(id_arr) as combined_id_arr
FROM
    RESULTS
GROUP BY 
    id

或更详细

SELECT
    id, 
    array_distinct(flatten(array_agg(id_arr))) as combined_id_arr
FROM
    RESULTS
GROUP BY 
    id

将与 UNNEST + ARRAY_AGG:

select id, array_agg(elements) as unique_elements
from ( select distinct id, elements from RESULTS
       cross join unnest(id_arr) as t(elements)
       order by elements ) a
group by id