Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVMfE-0001AK-AD for pgsql-general@arkaria.postgresql.org; Tue, 19 Jun 2018 19:51:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fVMfC-00065L-Gu for pgsql-general@arkaria.postgresql.org; Tue, 19 Jun 2018 19:51:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVMfC-00065C-90 for pgsql-general@lists.postgresql.org; Tue, 19 Jun 2018 19:51:38 +0000 Received: from mail-pl0-x236.google.com ([2607:f8b0:400e:c01::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fVMf8-0003LW-G8 for pgsql-general@lists.postgresql.org; Tue, 19 Jun 2018 19:51:37 +0000 Received: by mail-pl0-x236.google.com with SMTP id g20-v6so413554plq.1 for ; Tue, 19 Jun 2018 12:51:33 -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=FIsZhxX188LD+SCawUrNOyBOude9BA2HrsMhPSkk2+k=; b=WnP8NKwG5kiradovlwPiXPIN4xLiKgtrZl94bxy++TZdafq8+aKUeiNb2rhg8zU987 xHtJU1uoaHekVH72mVt72D9+X8stelR0Xr8jsHeEIJSwQS7MPqn0HGuM9QbCcOeUs+gi UBRTB2yd8O4RBNcnIUJlyry7/aBQIS4zd4e+QzlMpUErRzNwlbsnCYtkbqa5WW/3fy+G RrmLCTbsKwD/GQJJEyKfA3b22jI5i557IPkIh8Hz7/E7IpnYx5S+TnOU83CCQtT3tsOY Dij5CnzriAy8+a6uR4al3KJGKsrM/hElOQXx4NvGKaiJC1w8ZDFeCLlImKeVot+DS3Xh DW/Q== 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=FIsZhxX188LD+SCawUrNOyBOude9BA2HrsMhPSkk2+k=; b=UoezACSzpMIvjvReLqziTUysTmELYGfLiURRVprySAhswIQq4cIESwVYIfGDMpTyge RU5jNZlBXvskOtXcRi4LjUB/MixGo24GDU2ZhUpVRGI5GzFEqy6N9X+Iau5feSvn2ZrD MYSnsNH2+DC7jKa0XAJkyzqCZWlwbef8fJtfYnS35Z4ecn9JndwsUYw2SWH4vVznQ5nJ cWqpiw72JeuWQIQLMYb8FlqKYwuDdj+aJCaIlq+/F9EpBi7CWCyTg8/vtCdlXeRQgNlF 2qEcObt1M78m8YAal0O0Kcyoafj/AuwG4a4DAyTr3c5+KD94AJmdArUn1eXm5BeIW9KI XB8g== X-Gm-Message-State: APt69E2wW5O1XodnN6Sf3aeqKO3WvtpBSR/nI/Lo62gDXPcsMRYA6UqG +ttGP3hbSuzBPjlIc0JCh4Ejpf03 X-Google-Smtp-Source: ADUXVKKXUW1m+nY0Rm8RX6ELqXZcU0ishsKy869RKLe8cC57nN1F3hrAy9WSV2k5DzWb7AN6xZEMng== X-Received: by 2002:a17:902:822:: with SMTP id 31-v6mr20159264plk.172.1529437891422; Tue, 19 Jun 2018 12:51:31 -0700 (PDT) Received: from [10.104.134.14] ([155.100.47.6]) by smtp.gmail.com with ESMTPSA id i71-v6sm483787pgd.22.2018.06.19.12.51.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Jun 2018 12:51:30 -0700 (PDT) Subject: Re: Is postorder tree traversal possible with recursive CTE's? To: pgsql-general@lists.postgresql.org References: <5196CEBE-CF2E-48AF-A496-3CE2FE1B6FD2@gmail.com> From: Rob Sargent Message-ID: <0e9d7846-3443-a9a3-605f-bd680edea0b2@gmail.com> Date: Tue, 19 Jun 2018 13:51:29 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------8FF47D2A6FFF5E15984CB4E6" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------8FF47D2A6FFF5E15984CB4E6 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 06/19/2018 01:14 PM, Hellmuth Vargas wrote: > > Hi > > with partial sum: > > > > > with recursive pizza (name, step, ingredient, quantity, unit, rel_qty, > path, weight) > as ( >         select >                 name, step, ingredient, quantity, unit >         ,       quantity::numeric(10,2) >         ,       step::text >         ,       case when unit = 'g' then quantity::numeric(10,2) else > null end >           from recipe >          where name = 'pizza' >         union all >         select > recipe.name , recipe.step, recipe.ingredient, > recipe.quantity, recipe.unit >         ,       (pizza.rel_qty * recipe.quantity)::numeric(10,2) >         ,       pizza.path || '.' || recipe.step >         ,       case when recipe.unit = 'g' then (pizza.rel_qty * > recipe.quantity)::numeric(10,2) else null end >           from pizza >           join recipe on (recipe.name = > pizza.ingredient) > ) > select path, ingredient, quantity, rel_qty, unit, weight,*sum(weight) > over(partition by split_part(path,'.',1)) as parcial_weight*, > *sum(weight) over() as total_weight* >   from pizza >  order by path; > >  path  |  ingredient  | quantity | rel_qty | unit  | weight | > parcial_weight | total_weight > -------+--------------+----------+---------+-------+--------+----------------+-------------- >  1     | tomato sauce |     1.00 |    1.00 | pcs   |        |        >  113.00 |      313.00 >  1.1   | tomato  |   100.00 |  100.00 | g     | 100.00 |        >  113.00 |      313.00 >  1.2   | basil |    10.00 |   10.00 | g     |  10.00 |         113.00 > |      313.00 >  1.3   | salt  |     3.00 |    3.00 | g     |   3.00 |         113.00 > |      313.00 >  2     | pizza bottom |     1.00 |    1.00 | pcs   |        |        >  200.00 |      313.00 >  2.2   | dough |     1.00 |    1.00 | pcs   |        |         200.00 > |      313.00 >  2.2.1 | flour |   150.00 |  150.00 | g     | 150.00 |         200.00 > |      313.00 >  2.2.2 | water |    50.00 |   50.00 | g     |  50.00 |         200.00 > |      313.00 >  2.2.3 | salt  |     1.00 |    1.00 | pinch |        |         200.00 > |      313.00 > (9 rows) > > > > This is gorgeous but I suspect any level greater than 10 wide will present sorting problems, no?  Maybe a fixed two-digit, zero filled number per level? Pushing the problem off by an order of magnitude :) An exercise left to the OP perhaps. --------------8FF47D2A6FFF5E15984CB4E6 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit



On 06/19/2018 01:14 PM, Hellmuth Vargas wrote:

Hi

with partial sum:




with recursive pizza (name, step, ingredient, quantity, unit, rel_qty, path, weight)
as (
        select
                name, step, ingredient, quantity, unit
        ,       quantity::numeric(10,2)
        ,       step::text
        ,       case when unit = 'g' then quantity::numeric(10,2) else null end
          from recipe
         where name = 'pizza'
        union all
        select
                recipe.name, recipe.step, recipe.ingredient, recipe.quantity, recipe.unit
        ,       (pizza.rel_qty * recipe.quantity)::numeric(10,2)
        ,       pizza.path || '.' || recipe.step
        ,       case when recipe.unit = 'g' then (pizza.rel_qty * recipe.quantity)::numeric(10,2) else null end
          from pizza
          join recipe on (recipe.name = pizza.ingredient)
)
select path, ingredient, quantity, rel_qty, unit, weight,sum(weight) over(partition by split_part(path,'.',1)) as parcial_weight, sum(weight) over() as total_weight
  from pizza
 order by path;

 path  |  ingredient  | quantity | rel_qty | unit  | weight | parcial_weight | total_weight
-------+--------------+----------+---------+-------+--------+----------------+--------------
 1     | tomato sauce |     1.00 |    1.00 | pcs   |        |         113.00 |       313.00
 1.1   | tomato       |   100.00 |  100.00 | g     | 100.00 |         113.00 |       313.00
 1.2   | basil        |    10.00 |   10.00 | g     |  10.00 |         113.00 |       313.00
 1.3   | salt         |     3.00 |    3.00 | g     |   3.00 |         113.00 |       313.00
 2     | pizza bottom |     1.00 |    1.00 | pcs   |        |         200.00 |       313.00
 2.2   | dough        |     1.00 |    1.00 | pcs   |        |         200.00 |       313.00
 2.2.1 | flour        |   150.00 |  150.00 | g     | 150.00 |         200.00 |       313.00
 2.2.2 | water        |    50.00 |   50.00 | g     |  50.00 |         200.00 |       313.00
 2.2.3 | salt         |     1.00 |    1.00 | pinch |        |         200.00 |       313.00
(9 rows)




This is gorgeous but I suspect any level greater than 10 wide will present sorting problems, no?  Maybe a fixed two-digit, zero filled number per level? Pushing the problem off by an order of magnitude :)
An exercise left to the OP perhaps.


--------------8FF47D2A6FFF5E15984CB4E6--