Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with string_agg with distinct in postgres

Tags:

postgresql

I am getting error for below query. Basically to use || & distinct together.

select string_agg( 'pre' || distinct user.col, 'post') 

It works fine like this

select string_agg( 'pre' || user.col, 'post') 

& this

select string_agg(distinct user.col, 'post') 
like image 895
user1298426 Avatar asked Jul 16 '14 15:07

user1298426


People also ask

What is Array_agg in Postgres?

PostgreSQL ARRAY_AGG() function is an aggregate function that accepts a set of values and returns an array where each value in the input set is assigned to an element of the array.

What is String_agg in SQL Server?

STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated.


1 Answers

select string_agg(distinct 'pre' || user.col, 'post') 

As the above will deny the use of an index in the distinct aggregation take the 'pre' out

select 'pre' || string_agg(distinct user.col, 'postpre') 
like image 119
Clodoaldo Neto Avatar answered Sep 18 '22 17:09

Clodoaldo Neto