pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Is postorder tree traversal possible with recursive CTE's?
Date: Tue, 19 Jun 2018 13:51:29 -0600
Message-ID: <0e9d7846-3443-a9a3-605f-bd680edea0b2@gmail.com> (raw)
In-Reply-To: <CAN3Qy4pBy+A-+BCkjEC==Y0M1Rgr1MXOqtHwX68PFQjhqumZLQ@mail.gmail.com>
References: <5196CEBE-CF2E-48AF-A496-3CE2FE1B6FD2@gmail.com>
	<CAN3Qy4rPoEOAGWJjwSEgAzE=oLwxHzGuSqVrSD-Dz8Wp+jn-2Q@mail.gmail.com>
	<CAN3Qy4pBy+A-+BCkjEC==Y0M1Rgr1MXOqtHwX68PFQjhqumZLQ@mail.gmail.com>



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 <http://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 <http://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.

view thread (11+ messages)  latest in thread

Message-ID: <0e9d7846-3443-a9a3-605f-bd680edea0b2@gmail.com>
Permalink:  ../0e9d7846-3443-a9a3-605f-bd680edea0b2@gmail.com/
Also on:    postgresql.org/message-id/0e9d7846-3443-a9a3-605f-bd680edea0b2@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com
  Subject: Re: Is postorder tree traversal possible with recursive CTE's?
  In-Reply-To: <0e9d7846-3443-a9a3-605f-bd680edea0b2@gmail.com>
  References: <5196CEBE-CF2E-48AF-A496-3CE2FE1B6FD2@gmail.com> <CAN3Qy4rPoEOAGWJjwSEgAzE=oLwxHzGuSqVrSD-Dz8Wp+jn-2Q@mail.gmail.com> <CAN3Qy4pBy+A-+BCkjEC==Y0M1Rgr1MXOqtHwX68PFQjhqumZLQ@mail.gmail.com> <0e9d7846-3443-a9a3-605f-bd680edea0b2@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox