我有一个表,我想拉一行每个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).
假设表your_table有三列(name, id, value),查询如下:
select name,
array_to_string(array_agg(id), ','),
array_to_string(array_agg(value), ',')
from your_table
group by name
order by name
;
"TM67" "4,9,72" "32556,98200,22300"
"TM99" "2,3" "23009,11200"
KI