Quantcast
Viewing all articles
Browse latest Browse all 207

How to turn a set of flat trees into a single tree with multiple leaves?

We have this beautiful Postgres tree generator. Yet it kind of produces cuts of a tree not a whole tree all at once:

item_id jsonb_pretty1   {"title": "PARENT","item_id": 1,"children": {"title": "LEVEL 2","item_id": 2,"children": {"title": "LEVEL 3.2","item_id": 6        }    }}1   {"title": "PARENT","item_id": 1,"children": {"title": "LEVEL 2","item_id": 2,"children": {"title": "LEVEL 3.1","item_id": 3,"children": {"title": "LEVEL 4.1","item_id": 4            }        }    }}1   {"title": "PARENT","item_id": 1,"children": {"title": "LEVEL 2","item_id": 2,"children": {"title": "LEVEL 3.1","item_id": 3,"children": {"title": "LEVEL 4.2","item_id": 5            }        }    }}

I want to get a single tree object out from it with an array like this:

1   {"title": "PARENT","item_id": 1,"children": [{"title": "LEVEL 2","item_id": 2,"children": [{"title": "LEVEL 3.2","item_id": 6        },        {"title": "LEVEL 3.1","item_id": 3,"children": [{"title": "LEVEL 4.1","item_id": 4            },            {"title": "LEVEL 4.2","item_id": 5            }]        }]    }]}

Here is the generator:

CREATE TABLE items (  item_id     serial PRIMARY KEY,  title text);CREATE TABLE joins (  id          serial PRIMARY KEY,  item_id     int,  child_id    int);INSERT INTO items (item_id,title) VALUES  (1,'PARENT'),  (2,'LEVEL 2'),  (3,'LEVEL 3.1'),  (4,'LEVEL 4.1'),  (5,'LEVEL 4.2'),  (6,'LEVEL 3.2');INSERT INTO joins (item_id, child_id) VALUES  (1,2),  (2,3),  (3,4),  (3,5),  (2,6);WITH RECURSIVE t(item_id, json, level) AS (        SELECT item_id, to_jsonb(items), 1        FROM items        WHERE NOT EXISTS (                SELECT 2                FROM joins                WHERE items.item_id = joins.item_id        )        UNION ALL        SELECT parent.item_id, to_jsonb(parent) || jsonb_build_object( 'children', t.json ),               level + 1        FROM t        JOIN joins AS j                ON t.item_id = j.child_id        JOIN items AS parent                ON j.item_id = parent.item_id        WHERE level < 7)SELECT item_id, jsonb_pretty(json)FROM tWHERE item_id = 1;

How would I change such a generator to produce one single tree in Postgres 13+?


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>