0

With Postgresql I have the query:

select '1' , '2', '3';

with result

enter image description here

I would like to have next result:

column
1
2
3

How can I do that?

3 Answers 3

2

You could create an array and then use UNNEST():

SELECT UNNEST(ARRAY[1,2,3]);
Sign up to request clarification or add additional context in comments.

Comments

1

You are looking for Unpivot, then you can try to use UNION ALL

select '1' column
UNION ALL
SELECT '2'
UNION ALL
SELECT '3'

or you can try to use JOIN LATERAL

SELECT s.*
FROM test t
JOIN LATERAL (VALUES(t.a),(t.b),(t.c)) s(column) ON TRUE

sqlfiddle

Comments

1

I prefer to use a table function for that:

SELECT CAST(i AS text) FROM generate_series(1, 3) AS i;

7 Comments

It doesn't work with: SELECT i FROM ('1','2') AS i;
Oh, you want strings. Fixed.
SELECT CAST(i AS text) FROM ('1', '2') AS i; RESULT -> ERROR: syntax error at or near "1"
@DavidLeon: please read the answer: it uses from generate_series(...) not from ('1', '2')
@a_horse_with_no_name the question is with String
|

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.