【问题标题】:Merge two arrays of object from two different columns to create a new array of unique and not null objects in postgresql合并来自两个不同列的两个对象数组以在 postgresql 中创建一个新的唯一且非空对象数组
【发布时间】:2018-05-31 06:19:00
【问题描述】:

我有一个结果集,它返回 3 列,其中 1 列是 varchar,2 列是数组,现在我需要合并数组列以创建一个不为空唯一元素的新数组。我尝试了不同的选项,但它们都不起作用,有什么建议吗?

【问题讨论】:

    标签: sql arrays postgresql merge


    【解决方案1】:

    您可以将数组和unnest 连接成行。然后您可以使用distinct 获取唯一行,并使用array_agg 将它们组合回一个数组:

    select  id
    ,       array_agg(nr)
    from    (
            select  distinct id
            ,       unnest(array[col1] || col2 || col3) nr
            from    t1
            ) sub
    group by
            id
    

    Example at SQL Fiddle.

    【讨论】:

      【解决方案2】:

      谢谢大家的建议。我发现解决方案 array_cat 函数对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多