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 1m84HD-00023P-Mo for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 17:20:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m84HC-0008Q8-Dn for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 17:20:26 +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 1m84HC-0008Q0-2s for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 17:20:26 +0000 Received: from mail-ot1-x32c.google.com ([2607:f8b0:4864:20::32c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m84H5-00046R-Mx for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 17:20:25 +0000 Received: by mail-ot1-x32c.google.com with SMTP id o2-20020a9d22020000b0290462f0ab0800so5987494ota.11 for ; Mon, 26 Jul 2021 10:20:19 -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-transfer-encoding:content-language; bh=BZlI03Lm+7C7a2n7jpY8T+NvYY0Jt//cJU/tkEtit6U=; b=EemZg/vK56QTHZRXiK6n+uYIPutPX9e4BagoXko8JXN3a6PNRl1dKbW4H/rJTRj4Op 4oLmSrqjCxWfCw8YsSsDd8MHKRG2pBNLqgRJ4KHVYM8qFI4KyK50ok1YNFrJzt877Bj1 L8ToxChRUzbWfaaWRRVQaj/Jldj4SGDDqLRLopFpge6Bo1VH4AF6Y5XSqDSZ+Ph4Rl3F ICVJEfdnIoCPBk0oqY6LY8tPdIJX0I1UeNs12hZYwU8nSZ0Up6lrUwUmiFIl26OfyFyP auSP5Xf0AzK0UzsjuS2NXTWaHoBkr5DTRbwbaH/vxpURxdTAJtcuWfQ/nl1ZMhrV5W87 q0Nw== 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-transfer-encoding :content-language; bh=BZlI03Lm+7C7a2n7jpY8T+NvYY0Jt//cJU/tkEtit6U=; b=PrPFcLa3est1hYXLQSq87C1kJSCv68zhlm1ZX3t4LIPMELDmM7C+2tmzxpq1O1C6sm V4Rn2dkaZJ4cuwrDnqsZeOlAr+fQv6+DJTfjfbseon8lXbZHk4/jhsUxWHHQxedCq8p4 MNlxv75J+QoBP5oyaFgqedx1UqxYlDXRlQ4FzDRVr4wSHRizUEhJoZHVS7wOlfzmhOxF GLUZTS6JGad6GyBkylt7US9cp2drOb3xjwN15gjou44ppiTaAj6dLwMOQPDQo7YfvFUq MvFE1XeCLXRFxHXvsqCDS20tVc92OxsL4ZsUUoHX75klilDtgzDmkmPM12dkaFiGqhg1 zAuw== X-Gm-Message-State: AOAM532ZQzagrK509mNIw558bGPMexMGLgsZI6+3FkevVAbvDI3G07ET SnigbZxQM/rzxnoa9pDCSHtu1h1qoUI= X-Google-Smtp-Source: ABdhPJwCuX8+R75kUan79SWNwyrEa47+PK2a9YBI3Y3/RPnM2XT065PH/XwgQLjf3TM8CWOHipuSng== X-Received: by 2002:a05:6830:1658:: with SMTP id h24mr12234816otr.182.1627320017524; Mon, 26 Jul 2021 10:20:17 -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 w7sm89779oik.23.2021.07.26.10.20.16 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Jul 2021 10:20:17 -0700 (PDT) Subject: Re: Recursive Parent-Child Function Bottom Up To: pgsql-general@lists.postgresql.org References: <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com> <4FE3DD91-4494-4339-ABE2-21EA7C861059@gmail.com> From: Rob Sargent Message-ID: <637663b2-8929-90da-1495-87cfe539c6fd@gmail.com> Date: Mon, 26 Jul 2021 11:20:15 -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: <4FE3DD91-4494-4339-ABE2-21EA7C861059@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-CA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/26/21 9:55 AM, Alban Hertroys wrote: >> On 26 Jul 2021, at 17:52, Alban Hertroys 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)