0

I want to select from the table arrays of integers like this:

[1, 2, 3]

Now in trying something like this:

(SELECT array_to_json(array_agg(row_to_json(s))) FROM(
 SELECT specialty FROM talent_specialty WHERE userid = 840 )s);

and this is the record that query is returning

[{"specialty":1},{"specialty":2}]

Table look like this:

enter image description here

12
  • What is your expected output ? Commented Jan 16, 2019 at 15:00
  • @Tony just array of integers like this [1, 2, 3] Commented Jan 16, 2019 at 15:01
  • So just to make sure i understood. You have [{"specialty":1},{"specialty":2}, ...] and want [1,2,3,...]? Commented Jan 16, 2019 at 15:05
  • 1
    [{"specialty":1},{"specialty":2}, ...] is the value from your table? Or does your table contain columns with rows 1,2,3? It would be nice if you could add a sample table to your question Commented Jan 16, 2019 at 15:09
  • 1
    Yes this is what json_agg does (see linked demo and documentation) Commented Jan 16, 2019 at 15:15

1 Answer 1

1

Do you simply search for json_agg?

demo:db<>fiddle

SELECT json_agg(speciality) 
FROM talent_speciality

If you are not needing a JSON array but a simple array you could use array_agg of course

SELECT array_agg(speciality) 
FROM talent_speciality
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.