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 1m83HT-00082p-HZ for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 16:16:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m83HS-0003n9-GM for pgsql-general@arkaria.postgresql.org; Mon, 26 Jul 2021 16:16:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m83HS-0003n1-7Z for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 16:16:38 +0000 Received: from mail-ot1-x333.google.com ([2607:f8b0:4864:20::333]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m83HP-00025g-Hy for pgsql-general@lists.postgresql.org; Mon, 26 Jul 2021 16:16:37 +0000 Received: by mail-ot1-x333.google.com with SMTP id i39-20020a9d17270000b02904cf73f54f4bso8631645ota.2 for ; Mon, 26 Jul 2021 09:16:35 -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=FQSt94B+2s/ZwpYn1olGBuGorsrqN42ZeFxlZ9hPP+A=; b=OCHOUdHZt4Y394boG0TxH3/jwsihqywpd27ycu5SUT63zxaKZJEpCdnbgTPrN11FTL iGxwWqDft1IA6LNqNJWrSVTgfXr+u0za+86FLetOP9GIqxagbz9r6yzkxm2aqT3mV9AG gaihWvmoVKehGWdCS7L/5pq/XN5Tqz20227i4SYDsy2DUJf+eSkzV7HVthA7WYDpKF02 aLYcEWAUO6b+O9JJTX/Z0i8uY2dRpj82sTIVEwNGn+eD6W3GjMdM7N9rjLE6D+MQSn7m WHuiVHgbR5dkh9Bn9/bpyVR35AX7PYyLVrGor84op8WhVXL8qFs3/VXhnkaNM1ATEBhq xYHg== 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=FQSt94B+2s/ZwpYn1olGBuGorsrqN42ZeFxlZ9hPP+A=; b=nt/Xu+Z0X1HGCWUy/QOiJ3T6RWfWCp74rNpbySYZTRdXWq9Y1L0N8xQwAwgj9ifoyC UTSYa+elhm/8CRMfzL/WBAan1W5zONC2ePkiRX87I3KSxxg815qt2SxCyFCrInhkFRDR besykE6l1J1WtQ4decfzaX2OH0qwikYcuLH859BF7Yn+le4ePbH+B0pFehl0icVI2s1w xMWanQ+Uppx11E3Gn98Dx7ClH46n8mLlIyNS0+Kp4p9MWMnp/qBOGQGWCfAA+dxs7Xl5 eFtlzp8x/xR2m86VCmQvGqR3lP4J07T4rkVLIHGZvO22kTWtKytGffdab+YTUdan2G9r QznQ== X-Gm-Message-State: AOAM533lUBsPprKCsTEVcXOPnC5Sd96vhQ2T4XL1vNGoCWgGfYyEkKCq GWv3UjQ4aAUb14ZoPJQ5J0WxaRoc/SU= X-Google-Smtp-Source: ABdhPJxb7tL4L/G8oXglRWdILuwAMvoeVQKRV8WidtBT8v0kYHzv+G4uLazdcD/QlQu3x47bLnK/AA== X-Received: by 2002:a9d:6b8e:: with SMTP id b14mr12706239otq.103.1627316194377; Mon, 26 Jul 2021 09:16:34 -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 g1sm64923otk.21.2021.07.26.09.16.33 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Jul 2021 09:16:34 -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: <760c57be-5e75-b400-32cb-54b5240d9504@gmail.com> Date: Mon, 26 Jul 2021 10:16:32 -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: multipart/alternative; boundary="------------E8317C025AFE55321FCCF537" 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. --------------E8317C025AFE55321FCCF537 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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. > > > 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. --------------E8317C025AFE55321FCCF537 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
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.

--------------E8317C025AFE55321FCCF537--