2

select array[[1,2],[2,3]]

Output: -[ RECORD 1 ]-------- array | {{1,2},{2,3}}

How do I flat the array, so I can then unnest?

Expected {1, 2, 2, 3}

3
  • Why don't you simply use select array[1,2,3,4]? Commented Sep 4, 2020 at 5:14
  • @a_horse_with_no_name i can't change dataset, this is just a simplication Commented Sep 4, 2020 at 5:16
  • Then please show us the real data and what you really want to do (probably doing an UPDATE to change existing rows). Simplifying your question is a good thing, but simplifying it to a point where you won't get a helpful answer not so much Commented Sep 4, 2020 at 5:17

1 Answer 1

3

unnest() completely flattens the array. If you need the flattened array, then unnest() and follow with an array_agg()

select array_agg(el order by rn)
  from unnest(array[array[1,2],array[2,3]]) 
         with ordinality as a(el, rn);

 array_agg 
-----------
 {1,2,2,3}
(1 row)

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.