pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRecursive Parent-Child Function Bottom Up
7+ messages / 3 participants
[nested] [flat]
* Recursive Parent-Child Function Bottom Up
@ 2021-07-26 15:19 Avi Weinberg <AviW@gilat.com>
0 siblings, 3 replies; 7+ messages in thread
From: Avi Weinberg @ 2021-07-26 15:19 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
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...
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.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Recursive Parent-Child Function Bottom Up
@ 2021-07-26 15:43 Rob Sargent <robjsargent@gmail.com>
parent: Avi Weinberg <AviW@gilat.com>
2 siblings, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2021-07-26 15:43 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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?
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Recursive Parent-Child Function Bottom Up
@ 2021-07-26 15:46 Rob Sargent <robjsargent@gmail.com>
parent: Avi Weinberg <AviW@gilat.com>
2 siblings, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2021-07-26 15:46 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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.
oops, didn't see the third column in the create table statement.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Recursive Parent-Child Function Bottom Up
@ 2021-07-26 15:52 Alban Hertroys <haramrae@gmail.com>
parent: Avi Weinberg <AviW@gilat.com>
2 siblings, 1 reply; 7+ messages in thread
From: Alban Hertroys @ 2021-07-26 15:52 UTC (permalink / raw)
To: Avi Weinberg <AviW@gilat.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
> On 26 Jul 2021, at 17:19, Avi Weinberg <AviW@gilat.com> 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.
(…)
> 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);
First you need to figure out what your starting set of nodes is, and since you’re going to go bottom-up, those are your leaf nodes. Without any indicators for that though, you’ll have to determine that from a sub-query.
Something like this:
with recursive foo (id, parent, children_ids) as (
select id, parent, null::text
from tree t
where not exists (
select 1 from tree c where c.parent = t.id
)
union all
select t.id, t.parent
, f.id || case f.children_ids when '' then '' else ',’ end || f.children_ids
from foo f
join tree t on f.parent = t.id
where f.parent <> 0
;
Regards,
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Recursive Parent-Child Function Bottom Up
@ 2021-07-26 15:55 Alban Hertroys <haramrae@gmail.com>
parent: Alban Hertroys <haramrae@gmail.com>
0 siblings, 2 replies; 7+ messages in thread
From: Alban Hertroys @ 2021-07-26 15:55 UTC (permalink / raw)
To: Avi Weinberg <AviW@gilat.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
> On 26 Jul 2021, at 17:52, Alban Hertroys <haramrae@gmail.com> wrote:
> Something like this:
>
> with recursive foo (id, parent, children_ids) as (
> select id, parent, null::text
> from tree t
> where not exists (
> select 1 from tree c where c.parent = t.id
> )
> union all
> select t.id, t.parent
> , f.id || case f.children_ids when '' then '' else ',’ end || f.children_ids
> from foo f
> join tree t on f.parent = t.id
> where f.parent <> 0
> ;
Almost, the null::text in the initial select should of course be '’ in your case, and a unicode quote slipped into the last string of that case statement.
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Recursive Parent-Child Function Bottom Up
@ 2021-07-26 16:16 Rob Sargent <robjsargent@gmail.com>
parent: Alban Hertroys <haramrae@gmail.com>
1 sibling, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2021-07-26 16:16 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 7/26/21 9:55 AM, Alban Hertroys wrote:
>> On 26 Jul 2021, at 17:52, Alban Hertroys <haramrae@gmail.com> wrote:
>> Something like this:
>>
>> with recursive foo (id, parent, children_ids) as (
>> select id, parent, null::text
>> from tree t
>> where not exists (
>> select 1 from tree c where c.parent = t.id
>> )
>> union all
>> select t.id, t.parent
>> , f.id || case f.children_ids when '' then '' else ',’ end || f.children_ids
>> from foo f
>> join tree t on f.parent = t.id
>> where f.parent <> 0
>> ;
> Almost, the null::text in the initial select should of course be '’ in your case, and a unicode quote slipped into the last string of that case statement.
>
> Alban Hertroys
> --
> If you can't see the forest for the trees,
> cut the trees and you'll find there is no forest.
>
>
>
this might be what you want?
with recursive fulltree (id, parent, children_ids) as (
select id, parent, id::text as decsendants
from tree t
where not exists (
select 1 from tree c where c.parent = t.id
)
union all
select t.id,
t.parent,
f.id || case f.children_ids when '' then '' else ',' end ||
f.children_ids as descendants
from fulltree f
join tree t on f.parent = t.id
where f.parent != 0
)
select * from fulltree order by parent
;
I do think it breaks when there is more than one zero parent.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Recursive Parent-Child Function Bottom Up
@ 2021-07-26 17:20 Rob Sargent <robjsargent@gmail.com>
parent: Alban Hertroys <haramrae@gmail.com>
1 sibling, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2021-07-26 17:20 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 7/26/21 9:55 AM, Alban Hertroys wrote:
>> On 26 Jul 2021, at 17:52, Alban Hertroys <haramrae@gmail.com> wrote:
>> Something like this:
>>
>> with recursive foo (id, parent, children_ids) as (
>> select id, parent, null::text
>> from tree t
>> where not exists (
>> select 1 from tree c where c.parent = t.id
>> )
>> union all
>> select t.id, t.parent
>> , f.id || case f.children_ids when '' then '' else ',’ end || f.children_ids
>> from foo f
>> join tree t on f.parent = t.id
>> where f.parent <> 0
>> ;
> Almost, the null::text in the initial select should of course be '’ in your case, and a unicode quote slipped into the last string of that case statement.
>
> Alban Hertroys
> --
> If you can't see the forest for the trees,
> cut the trees and you'll find there is no forest.
>
>
>
well actually I find this closer to the target.
with recursive kids (founder, firstoff, descendant )
as (select parent as founder, id as firstoff, id::text
from tree
where parent = 0
union all
select k.firstoff, t.id, t.id::text
from kids k join tree t on k.firstoff = t.parent
)
select founder, array_agg(descendant) as descendants
from kids group by founder
order by founder;
founder | descendants
---------+-------------------------------
0 | {273}
273 | {274,275,277}
275 | {280}
277 | {278}
280 | {281}
281 | {282,290}
282 | {283,284,285,286,287,288,289}
290 | {291,292,293,294,295}
(8 rows)
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2021-07-26 17:20 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-07-26 15:19 Recursive Parent-Child Function Bottom Up Avi Weinberg <AviW@gilat.com>
2021-07-26 15:43 ` Rob Sargent <robjsargent@gmail.com>
2021-07-26 15:46 ` Rob Sargent <robjsargent@gmail.com>
2021-07-26 15:52 ` Alban Hertroys <haramrae@gmail.com>
2021-07-26 15:55 ` Alban Hertroys <haramrae@gmail.com>
2021-07-26 16:16 ` Rob Sargent <robjsargent@gmail.com>
2021-07-26 17:20 ` Rob Sargent <robjsargent@gmail.com>
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