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 1m82od-0006vk-GF for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:46:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m82oc-0006bc-Gm for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:46:50 +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 1m82oc-0006bU-6j for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:46:50 +0000 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m82oZ-0003Jn-Q0 for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:46:49 +0000 Received: by mail-pj1-x102a.google.com with SMTP id pf12-20020a17090b1d8cb0290175c085e7a5so20386573pjb.0 for ; Mon, 26 Jul 2021 08:46:47 -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=WmmLV8LstmO9kMu2T8WwNVMbXYy9tCoNBgna1piKh40=; b=kqGsBD+GGj4sRU59o/vBpob9fLQgwMq9cQPwGGF60LgOPEVAAJSS54ESrHtdRsfK3u Czv6ptSIymXgZU6Ei1DIdsheCgN5pvq8f97ZF35irsT32k+Vj2AzwCKxaUHFpthgVemd sWnNTQSzI6R/d1EqPpcpewobud/J/29ha0jXDu8WY7konQOAoJmrIeS5QXXhkVsqN4UE MViiBFsearK3nkmaQ7ssnV5d8z571LRQA1lI/JfJmcTcVbVtaWpRfPPOqZcotTmWgqoE x2lTtN3kX5h7RNb1RN+JXNRKaE2RqvZMuVN4KXSmpKP1LGg8Kzup2gBe6HMqYcFKMA1p c84w== 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=WmmLV8LstmO9kMu2T8WwNVMbXYy9tCoNBgna1piKh40=; b=H4ti7AFcplEDf/xNBmxFZX0DZ+/qjsYXLeHhJTn22ad1/mo1U4r3i5oa59s/GakxYC /xqRwuHcl5wpqHr1iYGXEAczLMwAB5oOU4IZzIIPH/Ejm6J1nsqImeBx1NHFLAekVaJC 7fVAD8FVkuQLtztRx22sB9G3uGc4HiLvCPoJiN53TGKVPKYjmUa5Rs7qJ9jHQ6a8ZjCn XhLabXJSbFFrekpvJhKAAgMEYKdPkXmzFmhQEWhv9ZGqJr/Qdor2JrL8A0R5POtkUF3O 9Tyh1uke1UORmtwk8TmB5UEJ7Q9zhdHEUWSZCkymMdEAQFy0+TweHoD412kHD9SSOLI0 Kwtg== X-Gm-Message-State: AOAM5323LEyDsfl8dVtImARolrjd5che7uhtAgzGCtRy62fmmMZiidXI uxTpw7TClblF0qZ9Vs+w973mfdTFfNM= X-Google-Smtp-Source: ABdhPJyS6Pko6nk8SV4PlW8HXYRNq1GHcOCWLOGT/lMo2VzdPq5hs5aiH8hPHABfd9afIuCsOtJryQ== X-Received: by 2002:aa7:8550:0:b029:32b:963f:f53b with SMTP id y16-20020aa785500000b029032b963ff53bmr18762097pfn.0.1627314405270; Mon, 26 Jul 2021 08:46:45 -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 s15sm368610pfw.207.2021.07.26.08.46.44 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Jul 2021 08:46:44 -0700 (PDT) Subject: Re: Recursive Parent-Child Function Bottom Up To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: <84105a75-f9cd-32f1-ce73-336e989b4ec0@gmail.com> Date: Mon, 26 Jul 2021 09:46:43 -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="------------00C34D1FC7C2E0FD14E2AD4F" 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. --------------00C34D1FC7C2E0FD14E2AD4F 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. oops, didn't see the third column in the create table statement. --------------00C34D1FC7C2E0FD14E2AD4F 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.
oops, didn't see the third column in the create table statement.

--------------00C34D1FC7C2E0FD14E2AD4F--