0

These two Postgres function calls return the same, whereas I would expect the latter to retain its nested array structure. How do I do that?

SELECT * FROM unnest('{1, 10, 100, 2, 11, 101}'::integer[]);

SELECT * FROM unnest('{{1, 10, 100}, {2, 11, 101}}'::integer[]);

I need this for manipulating array of arrays.

1

2 Answers 2

2

There is a wiki page about this. https://wiki.postgresql.org/wiki/Unnest_multidimensional_array

Besides, PostgreSQL has much more functions for JSON than arrays.

SELECT * FROM jsonb_array_elements(to_jsonb('{{1, 10, 100}, {2, 11, 101}}'::integer[]));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This looks great. How does it compare in speed with @S-Man's answer?
1

Using this solution: https://stackoverflow.com/a/8142998/3984221 from @LukasEklund and @ErwinBrandstetter

demo:db<>fiddle

SELECT array_agg(t.myarray[d1][d2])
FROM   mytable t,
       generate_subscripts(t.myarray,1) d1,
       generate_subscripts(t.myarray,2) d2
GROUP  BY d1
ORDER  BY d1

generate_subscript() generates a consecutive number list from 1 to the dimension size given by the second parameter.

2 Comments

Thanks! This looks great. How does it compare in speed with @crvv's answer?
The solution of crvv seems to be much faster (in this case, I am curious about real data) but does not give out an simple array but a json array. Comparison of speed (have a look at the costs) and output: dbfiddle.uk/…

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.