Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m82l4-0006oO-St for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:43:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m82l3-0002Mo-TZ for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:43:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m82l3-0002Mg-GR for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:43:09 +0000 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m82l0-0003HB-VH for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:43:09 +0000 Received: by mail-pl1-x636.google.com with SMTP id d1so4820768pll.1 for ; Mon, 26 Jul 2021 08:43:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=bONvDzu7W/fVXk0GySY+CguYSX/e0YbT5mGsePWKZuM=; b=jeC0gF0NLfOkoRxy3S0pbRgu9wczh59TFT3xkf4IOavOMk0o3flCWKQIh8Gl5ltmdb N14IF0hjDmTOQYb13yXcLTYFxZW28V2tvkBcIldhFjlNWyEuBfCa5HEwLJyPsi6kORVE 4ykeOpJJGfDfJ8c7TTu50N7/nxQwXDEQpcOEXtCC/HwjQsdTUni/KFYRqnBOCkwOWzvu HizKoqNxRRTGa9O35cCPPH3c1MZkk/7XiA3PVQyqtn27X9KoVHPh4bvnR9KeBx/kJtEN 2burIg+iE25Z0hVuLiJbZCQ2MC+EsUpD5/WH2cWR3LNJ/hNYeK++xxhDfPyt+N+1yr8d wK5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=bONvDzu7W/fVXk0GySY+CguYSX/e0YbT5mGsePWKZuM=; b=X+JfddR/nQeXW77Lwk98crKYgDvPK7uQMjrszHcTcN7XfRPNdTLVseQA4RKOCcTb2x G+CTMSanYOEH+JyQHLExg0rJFkLF3qDP4ggwmwGxIPaq4caYEUxr8gIR+tWfGYAtoiO6 5l7fYDBQiPRuaA/OEeiHWD1Aj3EFuE/OkAIrun0YMw8ZzcN+F2cSsW8GE27AxXv0lZCD ZtEMsv+kfhGogeUtC7w+qh/p0CYp7f2MFECoqDf47ewj9dmpwommGAJrPa+A96y2cwTS Spexj64UCIZRas8/41MkW0L/DmgPGk3sAluumfeMUIYl4Yclc0VRmSC7fI4LNeznmie+ 4iGA== X-Gm-Message-State: AOAM532MQ0j1y+zjb003zBwkSD1uxjNrFVGgfY/LOzSHh+zNUDOU15VM Pe7UXO6MfbiTDuQTDYs5IjMzvblQfIs= X-Google-Smtp-Source: ABdhPJw/ZH0uyblzEBpek6aAaf2ZNXilKXsG9j3KFw7Q2XylzYnAfULVFRDJITOBmEZIrl8f0bVsKQ== X-Received: by 2002:aa7:8806:0:b029:32a:8632:e35e with SMTP id c6-20020aa788060000b029032a8632e35emr19135324pfo.10.1627314184675; Mon, 26 Jul 2021 08:43:04 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:e024:7785:92c7:18d6? ([2601:681:5500:dde0:e024:7785:92c7:18d6]) by smtp.gmail.com with ESMTPSA id f16sm64437pgb.51.2021.07.26.08.43.04 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Jul 2021 08:43:04 -0700 (PDT) Subject: Re: Recursive Parent-Child Function Bottom Up To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: <5fd63f2e-4fc0-fff6-fbd3-ed340894e33c@gmail.com> Date: Mon, 26 Jul 2021 09:43:03 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------36CC287C13810D06C5948826" Content-Language: en-CA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------36CC287C13810D06C5948826 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit 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-weighted-average-of-node-va > > > 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? --------------36CC287C13810D06C5948826 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit
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-weighted-average-of-node-va

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?

--------------36CC287C13810D06C5948826--