I want to extract columns depending on their data type from a table.
From this table I want to only end up with columns containing only integers.
Price. | Food | Quantity |
---|---|---|
5 | Bread | 6 |
3 | Cereal | 7 |
This is the desired output:
Price. | Quantity |
---|---|
5 | 6 |
3 | 7 |
How would I go about doing this?
I have tried to use string_agg() to use the column names in a select statement but it did not create the output I desired.
select(
select
string_agg(column_name, ',')
from information_schema.columns
where table_name = 'table_name' and data_type = 'integer')
from table_name
2
Answers
You must use dynamic SQL for do it.
Sample:
Result:
After then execute this using EXECUTE command: