Handling of quantity in recursive query example

From: PG Doc comments form <noreply(at)postgresql(dot)org>
To: pgsql-docs(at)lists(dot)postgresql(dot)org
Cc: jason(at)banfelder(dot)net
Subject: Handling of quantity in recursive query example
Date: 2022-08-27 14:50:47
Message-ID: 166161184718.1235920.6304070286124217754@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/14/queries-with.html
Description:

I believe that the second query in section 7.8.2 (Recursive Queries) should
be modified to properly account for quantities. In the recursive term,
p.quantity should be multiplied by pr.quantity:

WITH RECURSIVE included_parts(sub_part, part, quantity) AS (
SELECT sub_part, part, quantity FROM parts WHERE part = 'our_product'
UNION ALL
SELECT p.sub_part, p.part, p.quantity * pr.quantity
FROM included_parts pr, parts p
WHERE p.part = pr.sub_part
)
SELECT sub_part, SUM(quantity) as total_quantity
FROM included_parts
GROUP BY sub_part

As currently written, if a car has four wheels, and each wheel has five
bolts, the whole example returns five bolts for the parts of a car. With the
proposed change, it will return 20 bolts.

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message PG Doc comments form 2022-08-27 18:56:26 Section 40.1 Overview of Event Trigger Behavior, and Table 40.1 missing needed details
Previous Message PG Doc comments form 2022-08-26 09:05:14 No backup history file found