I have a source (and I have no control on it) with a table like:
CREATE TABLE temp.badtable(
Id INTEGER
,Meta TEXT
,Field001 FLOAT
,Field002 FLOAT
,...
,Field720 FLOAT
);
Where fields names are predictable and types are homogeneous. I would like to normalize this table before processing its data. I would like to have a well-shaped table instead, it should look like:
CREATE TABLE temp.goodtable(
Id INTEGER
--,Meta TEXT -- For normalization sake
,FieldName TEXT
,FieldValue FLOAT
);
Is there a proper way to do so with PostgreSQL?
Update
I get a beggining of solution using JSON. This is not performent, but it achieves what I need:
WITH
A AS (
SELECT
Id
,row_to_json(M) AS Data
FROM
temp.badtable
),
B AS (
SELECT
Id
,row_to_json(json_each(Data)) AS Pair
FROM
A
)
SELECT
Id
,Pair->>'key' AS FieldKey
,Pair->>'value' AS FieldValue
FROM B;