1

Is there a way to iterate through an array in SQL not using stored procedures? For instance given:

a | b
--+------
a | {1,2}

I would like to flatten the array into individual rows as in:

a | b
--+---
a | 1
a | 2

Clarification: I am trying to do this in Postgres and am looking for a pure SQL solution as opposed to using stored procedures. Please correct me if I am wrong but I don't think this has been addressed in previous questions. Thanks!

3
  • What RDBMS? One possible way, and another Commented Mar 6, 2013 at 15:03
  • @muistooshort, Yes, my mistake. How do you unvote-to-delete? Commented Mar 7, 2013 at 13:55
  • @Ben: I don't think you can, maybe check meta.stackoverflow.com. Anyway, it takes five votes to close so hopefully people will be responsible and read the comments before agreeing with you :) I'm pretty sure we've all made bad close-votes. Commented Mar 7, 2013 at 18:21

1 Answer 1

2

Very simple

select a, unnest(b)
from t
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.