By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
WITH list (list_name) AS (VALUES 'A', 'B', 'C', 'D')
, A (ID1, values1) AS (VALUES (1, 'A'))
, B (ID2, values2) AS (VALUES (2, 'B'))
, C (ID3, values3) AS (VALUES (3, 'C'))
, D (ID4, values4) AS (VALUES (4, 'D'))
SELECT transposedID AS ID, transposedValues AS values FROM (
SELECT table_1.* FROM (
SELECT * FROM A,B,C,D
INNER JOIN list l1 ON 'A' in l1.list_name
INNER JOIN list l2 ON 'B' in l2.list_name
INNER JOIN list l3 ON 'B' in l3.list_name
INNER JOIN list l4 ON 'B' in l4.list_name
) AS table_1
) CROSS JOIN LATERAL (
VALUES
(ID1, values1),
(ID2, values2),
(ID3, values3),
(ID4, values4)
) AS Transposed(transposedID, transposedValues);
ID | VALUES |
---|---|
1 | A |
2 | B |
3 | C |
4 | D |