From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Recursive Parent-Child Function Bottom Up
Date: Mon, 26 Jul 2021 09:43:03 -0600
Message-ID: <5fd63f2e-4fc0-fff6-fbd3-ed340894e33c@gmail.com> (raw)
In-Reply-To: <DB9PR07MB718032F033C6D95B92E5BAAECBE89@DB9PR07MB7180.eurprd07.prod.outlook.com>
References: <DB9PR07MB718032F033C6D95B92E5BAAECBE89@DB9PR07MB7180.eurprd07.prod.outlook.com>
On 7/26/21 9:19 AM, Avi Weinberg wrote:
>
> Hi,
>
> I would like to populate the children_ids column with all the ids of
> the children recursively (+ grandchildren etc.)
>
> If I do it top-bottom I will end up doing extra work since there is no
> need to go all levels down if I can just compute my IMMEDIATE children
> "children_ids" and just concatenate all their lists.
>
> I looked at
> https://stackoverflow.com/questions/41376655/how-can-i-traverse-a-tree-bottom-up-to-calculate-a-weig...
> <https://stackoverflow.com/questions/41376655/how-can-i-traverse-a-tree-bottom-up-to-calculate-a-weig...;
>
> But was not able to get it to work on my case.
>
> Your assistance is most welcome!
>
> create table tree(id int primary key, parent int, children_ids text);
>
> insert into tree (id, parent) values
>
> (273, 0),
>
> (274, 273),
>
> (275, 273),
>
> (277, 273),
>
> (278, 277),
>
> (280, 275),
>
> (281, 280),
>
> (282, 281),
>
> (283, 282),
>
> (284, 282),
>
> (285, 282),
>
> (286, 282),
>
> (287, 282),
>
> (288, 282),
>
> (289, 282),
>
> (290, 281),
>
> (291, 290),
>
> (292, 290),
>
> (293, 290),
>
> (294, 290),
>
> (295, 290);
>
> IMPORTANT - This email and any attachments is intended for the above
> named addressee(s), and may contain information which is confidential
> or privileged. If you are not the intended recipient, please inform
> the sender immediately and delete this email: you should not copy or
> use this e-mail for any purpose nor disclose its contents to any person.
I'm sorry, what output format are you looking for? Perhaps an array of
all descendants for each founder? (founder int, descendants int[]). I
recommend a null for no-known-parent rather than zero. Or just founder
int, descendant int? With or without sub-trees?
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: robjsargent@gmail.com
Subject: Re: Recursive Parent-Child Function Bottom Up
In-Reply-To: <5fd63f2e-4fc0-fff6-fbd3-ed340894e33c@gmail.com>
References: <DB9PR07MB718032F033C6D95B92E5BAAECBE89@DB9PR07MB7180.eurprd07.prod.outlook.com> <5fd63f2e-4fc0-fff6-fbd3-ed340894e33c@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox