如何从所有记录所有的整数数组合并到Postgres里,单阵列 [英] How to merge all integer arrays from all records into single array in postgres

查看:132
本文介绍了如何从所有记录所有的整数数组合并到Postgres里,单阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列是整型数组。我该如何合并他们都到一个整数数组?

I have a column which is of type integer array. How can I merge all of them into a single integer array?

For example: If I execute query: 

`select column_name from table_name`

I get result set as:

-[RECORD 1]----------
column_name | {1,2,3}
-[RECORD 2]----------
column_name | {4,5}

我怎样才能得到{1,2,3,4,5}作为最终的结果?

How can I get {1,2,3,4,5} as final result?

推荐答案

您可以使用<一个href=\"http://www.postgresql.org/docs/current/interactive/functions-array.html#ARRAY-FUNCTIONS-TABLE\"><$c$c>unnest开拓阵列,然后<一个href=\"http://www.postgresql.org/docs/current/interactive/functions-aggregate.html#FUNCTIONS-AGGREGATE-TABLE\"><$c$c>array_agg把他们重新走到一起:

You could use unnest to open up the arrays and then array_agg to put them back together:

select array_agg(c)
from (
  select unnest(column_name)
  from table_name
) as dt(c);

这篇关于如何从所有记录所有的整数数组合并到Postgres里,单阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆