I have this demo query that starts from a list of "author + articles" values:
VALUES
('author1', ARRAY[('title1', 'content1'), ('title2', 'content2')]),
('author2', ARRAY[('title3', 'content3'), ('title4', 'content4')])
I'd like to convert this into the following data structure:
| author | title | content |
|---|---|---|
| author1 | title1 | content1 |
| author1 | title2 | content2 |
| author2 | title3 | content3 |
| author2 | title4 | content4 |
I've come this far:
WITH "raw_data" ("author", "articles") AS (
VALUES
('author1', ARRAY[('title1', 'content1'), ('title2', 'content2')]),
('author2', ARRAY[('title3', 'content3'), ('title4', 'content4')])
),
"articles" AS (
SELECT "author", unnest("articles") AS "article" FROM "raw_data"
)
SELECT author, article FROM "articles";
But I get the following:
| author | article |
|---|---|
| author1 | (title1, content1) |
| author1 | (title2, content2) |
| author2 | (title3, content3) |
| author2 | (title4, content4) |
And I need to find a way to convert the record (title2, content1) into 2 distinct columns.
Thank you all!