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 1m82xW-0007Fd-Fv for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:56:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m82xV-00008i-El for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 15:56:01 +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 1m82xV-00007j-6A for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:56:01 +0000 Received: from mail-ej1-x62c.google.com ([2a00:1450:4864:20::62c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m82xT-0003OF-Be for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 15:56:00 +0000 Received: by mail-ej1-x62c.google.com with SMTP id e19so17086158ejs.9 for ; Mon, 26 Jul 2021 08:55:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=+tHYhfOsBEzbqCDAPbQIlmVjjQgXv0aVneclgqihLNw=; b=Ss2qlE4OwdFbaDb7CvWLGhKDWqVL4x42vRX4BlFHwN5NqdmISlnLCdswKb9PYAgc34 OuPptHna6P96yoAL9dRSX6KxoXJred3oOI0RohkSg+I0vi/nEr6V1mitdLysJgNANNby ECULcJE63ann5oR1g4cnz4A8Nsk4zzsP/3XEo39W+Q0/jhjpZWV2gxctH8So9sOUqXdQ anYV+rs6wNF06lQReNmX18MbvwsHTEOIRsBlFat5TVwKRPC/ysn6X/2/Ca7azTJ3eq3j wmfZZZG9zwo4gQzrPI+quJQY84HchzMLNDeXfKR+8NZhDHrG9HHW4H3UXgxfZW6sAQoq i+2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=+tHYhfOsBEzbqCDAPbQIlmVjjQgXv0aVneclgqihLNw=; b=EgFcSfxBtt/rvWcW5ocHOPseBhcp0gsKr41iwjXyeb/0qytw7jeAHCN/G+NnZRyhKo kDlGi8w/m3QXsWmFJLAKSTNzE3uiTFxiJbaNvIx+/u3lwQToqwGk3EvITNNElpPCdp+X gra/wTv/5sBkeH+/PTIP0rFE/tZqQsSQFQdvvlDY8mW7QmbtJzzIyiJ4vFCYaSBYhzHU LPZdtPFIdu87te5g7r95jl9x6vDFdXHDbP3MCk5Yz8ccWzffkYuPkBzLxH/BC5Uqar1U KDzfXENjAlIyMBKh2X/eYP/WGKf5GRjsYdwPHJjOxjM5GcxP/6UabohBUCco8gJYYTBy H9Kg== X-Gm-Message-State: AOAM533j24LqBTy1qJqoQd9ngyfrUPCIBWJLF0MCZt/x8/h8sOvDK7CR D8OZpE0PpCpsKyFFJnrX3trB9b7+aGE= X-Google-Smtp-Source: ABdhPJyOzb3St5UD+BGo0Ru80G1+OzAmTZUIG9m8L6W4qILqcQOOvzT3Z/tZKei0MEJR+bDCbl7m1g== X-Received: by 2002:a17:906:63ca:: with SMTP id u10mr17143123ejk.411.1627314958462; Mon, 26 Jul 2021 08:55:58 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id k3sm120866edv.2.2021.07.26.08.55.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 26 Jul 2021 08:55:58 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: Recursive Parent-Child Function Bottom Up From: Alban Hertroys In-Reply-To: <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com> Date: Mon, 26 Jul 2021 17:55:57 +0200 Cc: "pgsql-general@lists.postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: <4FE3DD91-4494-4339-ABE2-21EA7C861059@gmail.com> References: <903EB164-89AA-4CB4-A031-A707954E6B23@gmail.com> To: Avi Weinberg X-Mailer: Apple Mail (2.3445.104.17) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 26 Jul 2021, at 17:52, Alban Hertroys wrote: > Something like this: >=20 > 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 =3D t.id > ) > union all > select t.id, t.parent > , f.id || case f.children_ids when '' then '' else ',=E2=80=99= end || f.children_ids > from foo f > join tree t on f.parent =3D t.id > where f.parent <> 0 > ; Almost, the null::text in the initial select should of course be '=E2=80=99= 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.