37

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')

3 Answers 3

62
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')
Sign up to request clarification or add additional context in comments.

1 Comment

This would prepend a single "pre" string to the entire string_agg output, eg preFIRSTpostSECONDpostTHIRDpost. The original question (although not very clear) appears to request prepending "pre" to each item included within the aggregation, eg preFIRSTpostpreSECONDpostpreTHIRDpost.
4

The concat function can help you.

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

Comments

3
array_to_string(array_agg(distinct column_name::text), '; ')

Will get the job done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.