mgig
mgig

Reputation: 2915

How do I return a table of rows from a list in postgresql?

I'm trying to generate rows given a list of values in postgresql. For example, if I have the values 1, 2, 5, 10, 30, 180, I would like to return:

num
----
1
2
5
10
30
180

I've been trying to use the VALUES function, but haven't had any luck. Here's an example of a failed atttempt:

SELECT num
FROM
VALUES (1, 2, 5, 10, 30, 180) as num

Upvotes: 0

Views: 39

Answers (1)

user330315
user330315

Reputation:

values (1, 2, 5, 10, 30, 180) returns a single row with 6 columns.

But you want six rows with one column:

SELECT num
FROM (
   VALUES (1), (2), (5), (10), (30), (180) 
) as t(num)

as t(num) assigns an alias to the derived table (the values part) which is named t and has a single column num.

Using the table alias in the select list returns a record, not a column.

Upvotes: 2

Related Questions