[sql] Postgresql GROUP_CONCAT equivalent?

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?

This question is related to sql postgresql group-concat string-aggregation

The answer is


Hope below Oracle query will work.

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

SELECT array_to_string(array(SELECT a FROM b),', ');

Will do as well.


Try like this:

select field1, array_to_string(array_agg(field2), ',')
from table1
group by field1;

Since 9.0 this is even easier:

SELECT id, 
       string_agg(some_column, ',')
FROM the_table
GROUP BY id

My sugestion in postgresql

SELECT cpf || ';' || nome || ';' || telefone  
FROM (
      SELECT cpf
            ,nome
            ,STRING_AGG(CONCAT_WS( ';' , DDD_1, TELEFONE_1),';') AS telefone 
      FROM (
            SELECT DISTINCT * 
            FROM temp_bd 
            ORDER BY cpf DESC ) AS y
      GROUP BY 1,2 ) AS x   

and the version to work on the array type:

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

Examples related to sql

Passing multiple values for same variable in stored procedure SQL permissions for roles Generic XSLT Search and Replace template Access And/Or exclusions Pyspark: Filter dataframe based on multiple conditions Subtracting 1 day from a timestamp date PYODBC--Data source name not found and no default driver specified select rows in sql with latest date for each ID repeated multiple times ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database

Examples related to postgresql

Subtracting 1 day from a timestamp date pgadmin4 : postgresql application server could not be contacted. Psql could not connect to server: No such file or directory, 5432 error? How to persist data in a dockerized postgres database using volumes input file appears to be a text format dump. Please use psql Postgres: check if array field contains value? Add timestamp column with default NOW() for new rows only Can't connect to Postgresql on port 5432 How to insert current datetime in postgresql insert query Connecting to Postgresql in a docker container from outside

Examples related to group-concat

How to use GROUP_CONCAT in a CONCAT in MySQL GROUP_CONCAT ORDER BY GROUP_CONCAT comma separator - MySQL MySQL DISTINCT on a GROUP_CONCAT() MySQL and GROUP_CONCAT() maximum length Postgresql GROUP_CONCAT equivalent? Can I concatenate multiple MySQL rows into one field? How to concatenate text from multiple rows into a single text string in SQL server?

Examples related to string-aggregation

How to make a query with group_concat in sql server SQL Query to concatenate column values from multiple rows in Oracle Postgresql GROUP_CONCAT equivalent? How do I create a comma-separated list using a SQL query? How can multiple rows be concatenated into one in Oracle without creating a stored procedure? How can I combine multiple rows into a comma-delimited list in Oracle? Simulating group_concat MySQL function in Microsoft SQL Server 2005? How to concatenate strings of a string field in a PostgreSQL 'group by' query?