2

I have a table full of records and an array column, and now I want to get a list of all (unique) values in that column. What's the best way of getting that?

I tried playing around with unnest, array_agg and string_agg but didn't get anywhere...

(using Postgres 9.2)

1
  • Updated answer after rereading you question. Commented Jul 15, 2013 at 13:44

1 Answer 1

6
select distinct unnest(a)
from (values
    (array[1, 2]),
    (array[2, 3])
) s(a);
 unnest 
--------
      3
      1
      2

Or aggregated in an array:

select array_agg(a order by a)
from (
    select distinct unnest(a) as a
    from (values
        (array[1, 2]),
        (array[2, 3])
    ) s(a)
) s;
 array_agg 
-----------
 {1,2,3}
Sign up to request clarification or add additional context in comments.

3 Comments

So there is no way to do this in one go or without a subquery?
@Manuel Not without creating an special aggregate function.
I looked at your first version and actually that is what I need (one row per result). Could you update your post once more and include both versions, for future readers? Thanks!

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.