user3814846
user3814846

Reputation:

How to dynamically create columns and rows using a select query in postgresql

for example,

select 1,2,3 ;

will return 3 columns with 1 row i.e [1,2,3]
, So how to get 3 coulmns with 3 rows with the same content ?

Upvotes: 2

Views: 1395

Answers (2)

Vivek S.
Vivek S.

Reputation: 21915

Try UNION, I think you need something like this

select 1 c1,2 c2,3 c3
     union  all
select 1 ,2 ,3 
     union  all
select 1 ,2 ,3 

Upvotes: 0

vyegorov
vyegorov

Reputation: 22885

Well, CROSS JOIN is a good fit here:

SELECT rownum, cols.*
  FROM (SELECT 1,2,3) cols
 CROSS JOIN generate_series(1,10) id(rownum);

You use SELECT cols.* if you need just values without row numbers.

Upvotes: 1

Related Questions