I have a Postgresql database with the following tables and relations (fiddle):
page: id, name
book: id, page.id
chapter: id, page.id, book.id
section: id, page.id, chapter.id
I’d like to create a (materialized) view that is able to associate every page to the names of his "ancestors", for example, with the following data:
Books:
id | pageid |
---|---|
1 | 1 |
Chapters:
id | pageid | bookid |
---|---|---|
2 | 2 | 1 |
Sections:
id | pageid | chapterid |
---|---|---|
3 | 3 | 2 |
Pages:
id | name |
---|---|
1 | foo |
2 | bar |
3 | baz |
Should return something like this:
pageid | bookname | chaptername | sectionname |
---|---|---|---|
1 | foo | null | null |
2 | foo | bar | null |
3 | foo | bar | baz |
I tried the following:
SELECT
book_page.name AS bookname,
chapter_page.name as chaptername,
section_page.name as sectionname
FROM (
SELECT
book.pageid AS bookpageid,
chapter.pageid AS chapterpageid,
section.pageid AS sectionpageid
FROM book
LEFT JOIN chapter ON chapter.bookid = book.id
LEFT JOIN section ON section.chapterid = chapter.id
) AS hierarchy
LEFT JOIN page AS book_page ON hierarchy.bookpageid = book_page.id
LEFT JOIN page AS chapter_page ON hierarchy.chapterpageid = chapter_page.id
LEFT JOIN page AS section_page ON hierarchy.sectionpageid = section_page.id;
but I only get:
bookname | chaptername | sectionname |
---|---|---|
foo | bar | ba |
2
Answers
Ok, I might have figured it out:
Probably can be optimized, but for a materialized view I thinks it does the job
Try this
View