I am struggling a bit on some dynamic postgresql : I have a table named "list_columns" containing the columns names list with "column_name" as the variable name; those column names come from an input table called "input_table".
[list_columns]
| column_name |
|---|
| col_name_a |
| col_name_b |
| col_name_c... |
[input_table]
| col_name_a | col_name_b | col_name_c | col_name_d | col_name_e |
|---|---|---|---|---|
| value_a_1 | value_b_1 | value_c_1 | value_d_1 | value_e_1 |
| value_a_2 | value_b_2 | value_c_2 | value_d_2 | value_e_2 |
| ... | ... | ... | ... | ... |
What I'd like to do is dynamically create a new table using that list, something like this:
create table output_table as
select (select distinct(column_name) seperated by "," from list_columns) from input_table;
The resulting table would be [output_table]
| col_name_a | col_name_b | col_name_c |
|---|---|---|
| value_a_1 | value_b_1 | value_c_1 |
| value_a_2 | value_b_2 | value_c_2 |
| ... | ... | ... |
I saw I should use some execute procedures but I can't figure out how to do so.
Note: I know i could directly select the 3 columns; I oversimplied the case.
If someone would be kind enough to help me on this,
Thank you, Regards, Jonathan