I am trying to unpivot a large dataset, with 250 columns. There is a very good documented solution here unpivot and PostgreSQL.
However, it inputs the column names manually. I'm looking to do something like..
- extract all column names into an array
- pass the array through unnest
OR,
- extract all column names into an array
- loop the array by indexing through
- using column name values as an input in the unnest
Apologies for being noob, New to SQL!
This dataset is good enough for purposes:
CREATE TEMP TABLE foo (id int, a text, b text, c text);
INSERT INTO foo VALUES (1, 'ant', 'cat', 'chimp'), (2, 'grape', 'mint', 'basil');
SELECT id,
unnest(array['a', 'b', 'c']) AS colname,
unnest(array[a, b, c]) AS thing
-- I would like something like.. unnest(array[column_names]) AS thing
-- where column_names = [a,b,c.. so on]
FROM foo
ORDER BY id;
Expected outcome:
id | colname | thing
1 | a | ant
1 | b | cat
1 | c | chimp
2 | a | grape
2 | b | mint
2 | c | basil