Using postgresql, i try to transform/pivot my data in table 1 (wide format) to a new destination table 2 in a long format, without success :
A simplified example of my problem :
Table 1
id_profil | no_h | P1 | P2 | P3 | Pn
01 1 5 7 x1 ...
01 2 7 78 x2 ...
02 1 5 7 x3 ...
Table 2 , result of Table 1 transformation :
id_profil | no_h | parametre | valeur
01 1 P1 5
01 1 P2 7
01 1 P3 x1
01 2 P1 7
01 2 P2 78
01 2 P3 x2
02 1 P1 5
02 1 P2 7
02 1 P3 x3
You can find and use the table 1 structure/data in this sqlfiddle.
I see in some stackoverflow posts that it is possible to use INNER JOIN LATERAL
to do that. (See Postgres analogue to CROSS APPLY in SQL Server.) But how do I inject the correct column names into the parameter columns?
Update
In the real database, i have more than 150 columns, so if it's possible to not enter each column name manually in the query, it's probably better.