gpt4 book ai didi

arrays - Postgres GROUP BY 整数数组

转载 作者:行者123 更新时间:2023-11-29 11:24:12 24 4
gpt4 key购买 nike

我在 Postgres 9.3 中的数据库表

| f1|f2 | f3
| 1 | 2 | {1,2,3}
| 1 | 3 | {4,5}

f1,f2 是整数 f3 是整数[].

我怎样才能得到这个:

SELECT f1, myfn(f3)
FROM dbTable
GROUP BY f1;

所以我得到:

| 1 | {1,2,3,4,5}

Postgres中有没有类似array_agg的函数,连接数组并创建一个新数组;

最佳答案

SQL Fiddle

unnest再聚合

select f1, array_agg(f3) as f3
from (
select f1, unnest(f3) as f3
from dbtable
) s
group by f1

为了避免重复和排序输出(必须安装内部扩展):

select f1, sort(uniq(array_agg(f3))) as f3
from (
select f1, unnest(f3) as f3
from dbtable
) s
group by f1

关于arrays - Postgres GROUP BY 整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com