1

I need to concatenate string returned by jsonb_array_elements() as a single column in my result.

I am using PostgreSQL v.12.1

Her my SQL:

SELECT
  name as name,
  jsonb_array_elements(payload -> 'pr' -> 'act') -> 'tags' -> 'Content' ->> 0 AS ws
FROM
  data

Current produce this result:

name   ws   
  A       1
  A       2
  A       3
  B       4

I would like to have this result instead:

name   ws
 A       1, 2, 3
 B       4

1 Answer 1

1

One way is to wrap your query with an outer query:

select name, string_agg(ws, ', ') from (<your query here>) foo group by name

Another would be to move the function call from the select-list into the from-list (as a lateral join, but LATERAL is implicit with function calls), which should make it available for the aggregate. You didn't give CREATE TABLE or INSERT statements with example data, and I'd rather not post something I can't test, so no example of this is provided.

Sign up to request clarification or add additional context in comments.

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.