我有一个表,我想拉一行每个id与字段值连接。

例如,在我的表格中,我有这样的:

TM67 | 4  | 32556
TM67 | 9  | 98200
TM67 | 72 | 22300
TM99 | 2  | 23009
TM99 | 3  | 11200

我想输出:

TM67 | 4,9,72 | 32556,98200,22300
TM99 | 2,3    | 23009,11200

在MySQL中,我能够使用聚合函数GROUP_CONCAT,但这似乎在这里不起作用…PostgreSQL有类似的功能吗,或者有其他方法可以实现这个功能?


当前回答

这可能是一个很好的起点(仅限8.4+版本):

SELECT id_field, array_agg(value_field1), array_agg(value_field2)
FROM data_table
GROUP BY id_field

array_agg返回一个数组,但是可以将其CAST为文本并根据需要进行编辑(参见下面的说明)。

在8.4版本之前,你必须在使用之前自己定义:

CREATE AGGREGATE array_agg (anyelement)
(
    sfunc = array_append,
    stype = anyarray,
    initcond = '{}'
);

(转述自PostgreSQL文档)

澄清:

The result of casting an array to text is that the resulting string starts and ends with curly braces. Those braces need to be removed by some method, if they are not desired. Casting ANYARRAY to TEXT best simulates CSV output as elements that contain embedded commas are double-quoted in the output in standard CSV style. Neither array_to_string() or string_agg() (the "group_concat" function added in 9.1) quote strings with embedded commas, resulting in an incorrect number of elements in the resulting list. The new 9.1 string_agg() function does NOT cast the inner results to TEXT first. So "string_agg(value_field)" would generate an error if value_field is an integer. "string_agg(value_field::text)" would be required. The array_agg() method requires only one cast after the aggregation (rather than a cast per value).

其他回答

根据我的经验,我使用bigint作为列类型。所以下面的代码为我工作。我使用的是PostgreSQL 12。

类型转换发生在这里。(::文本)。

string_agg(some_column::text, ',')

试着这样做:

select field1, array_to_string(array_agg(field2), ',')
from table1
group by field1;
SELECT array_to_string(array(SELECT a FROM b),', ');

也行。

以及用于数组类型的版本:

select
  array_to_string(
    array(select distinct unnest(zip_codes) from table),
    ', '
);

希望下面的Oracle查询能正常工作。

Select First_column,LISTAGG(second_column,',') 
    WITHIN GROUP (ORDER BY second_column) as Sec_column, 
    LISTAGG(third_column,',') 
    WITHIN GROUP (ORDER BY second_column) as thrd_column 
FROM tablename 
GROUP BY first_column