Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent for LISTAGG (Oracle database) in PostgreSQL?

I have to replace the Oracle driver with the newest PostgreSQL. PostgreSQL doesn't know the function LISTAGG. I have to concat values by comma separated. What's the equivalent for the Oracle's function LISTAGG in PostgreSQL?

like image 646
cy221 Avatar asked Apr 10 '15 09:04

cy221


People also ask

What can be used instead of Listagg?

In order to concatenate field values, I would use “GROUP_CONCAT” function in Virtual DataPort Administration tool which is similar to LISTAGG function. For example, GROUP_CONCAT('<row separator>',<field_name>)

What is Oracle Listagg?

An Oracle LISTAGG Function is an aggregate function that returns a single row. This is used to transform data from multiple rows into a single list of values separated by a given delimiter. It operates on all rows and returns single. It returns a comma or other delimiter separatedresult set just like an excel CSV file.

Is Postgres an Oracle database?

Oracle database management systems, the main difference between these two databases is that PostgreSQL is an open-source database, while Oracle is a closed database system. PostgreSQL is a free relational object-oriented database management system that is developed by volunteer developers worldwide.

Is PostgreSQL compatible with Oracle?

One of the biggest advantages of choosing the EnterpriseDB version of PostgreSQL is its Oracle compatibility.


1 Answers

The equivalent function in PostgreSQL is STRING_AGG()

SELECT STRING_AGG (column_name,', ')  FROM my_table 

string_agg : input values concatenated into a string, separated by delimiter

For example, get list of all agreement_id then represent it in a string, in Apache Ofbiz 17.12.04

SELECT STRING_AGG(agreement_id, ', ') FROM agreement_item;  -- result -- "8000, DS-1000-SALES, DS-1000-PURCH, 9000, AGR_SALES" 
like image 141
Vivek S. Avatar answered Oct 07 '22 16:10

Vivek S.