2

Consider following case

Table : tab1
id    serial      primary key
arr   int[]

Now I want to select each value of arr.

SELECT * FROM (SELECT arr FROM tab1) AS tab2

I need kind of iteration in array.

e.g.

id    arr
-----------------------------
1     [1,2]
2     [5,6,8]

So I could get result as

arr      val
-------------------------------
[1,2]    1
[1,2]    2
[5,6,8]  5
[5,6,8]  6
[5,6,8]  8

2 Answers 2

1

Use unnest() for that:

WITH array_data(id,arr) AS ( VALUES
  (1,ARRAY[1,2]),
  (2,ARRAY[5,6,8])
)
SELECT arr,unnest(arr) AS val 
FROM array_data;
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know if I've got well but here you have all you need

select id,
       unnest(arr),
       array_to_string(arr,','),
       array_length(arr, 1)
from array_data;

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.