I have a table and I'd like to pull one row per id with field values concatenated.
In my table, for example, I have this:
TM67 | 4 | 32556
TM67 | 9 | 98200
TM67 | 72 | 22300
TM99 | 2 | 23009
TM99 | 3 | 11200
And I'd like to output:
TM67 | 4,9,72 | 32556,98200,22300
TM99 | 2,3 | 23009,11200
In MySQL I was able to use the aggregate function GROUP_CONCAT
, but that doesn't seem to work here... Is there an equivalent for PostgreSQL, or another way to accomplish this?
9条答案
按热度按时间baubqpgj1#
Since 9.0 this is even easier:
gjmwrych2#
This is probably a good starting point (version 8.4+ only):
array_agg returns an array, but you can CAST that to text and edit as needed (see clarifications, below).
Prior to version 8.4, you have to define it yourself prior to use:
(paraphrased from the PostgreSQL documentation)
Clarifications:
tmb3ates3#
Will do as well.
368yc8dk4#
这样试试看:
uwopmtnx5#
Assuming that the table your_table has three columns (name, id, value), the query is this one:
KI
3zwtqj6y6#
和要在数组类型上工作的版本:
tsm1rwdh7#
我在postgresql的建议
neekobn88#
In my experience, I had bigint as column type. So The below code worked for me. I am using PostgreSQL 12.
Type cast is happening here. (::text).
xvw2m8pv9#
Hope below Oracle query will work.