问题
I would have expected that the following query returns all people with their respective children.
WITH RECURSIVE nested_people (id, name, children) AS (
SELECT id, name, NULL::JSON AS children
FROM people
WHERE parent_id IS NULL
UNION ALL
SELECT people.id, people.name, ROW_TO_JSON(nested_people.*) AS children
FROM people
JOIN nested_people ON people.parent_id = nested_people.id
)
SELECT * FROM nested_people;
But actually it does the exact reverse. I can't think of a way to do correct nesting without having to an additional CTE. Is there a way?
Example data
+----+-------+-----------+
| id | name | parent_id |
+----+-------+-----------+
| 1 | Adam | null |
| 2 | Abel | 1 |
| 3 | Cain | 1 |
| 4 | Enoch | 3 |
+----+-------+-----------+
Result
+----+-------+--------------------------------------------------------------------------+
| id | name | children |
+----+-------+--------------------------------------------------------------------------+
| 1 | Adam | null |
| 2 | Abel | {"id":1,"name":"Adam","children":null} |
| 3 | Cain | {"id":1,"name":"Adam","children":null} |
| 4 | Enoch | {"id":3,"name":"Cain","children":{"id":1,"name":"Adam","children":null}} |
+----+-------+--------------------------------------------------------------------------+
Expected Result
+----+-------+----------------------------------------------------------------------------------------------------------------------+
| id | name | children |
+----+-------+----------------------------------------------------------------------------------------------------------------------+
| 1 | Adam | [{"id":2, "name":"Abel", "children":null},{"id":3,"name":"Cain","children":[{"id":4,"name":"Enoch","children":null}] |
| 2 | Abel | null |
| 3 | Cain | [{"id":4,"name":"Enoch","children":null}] |
| 4 | Enoch | null |
+----+-------+----------------------------------------------------------------------------------------------------------------------+
回答1:
This rCTE traverses the tree from the other side:
WITH RECURSIVE cte AS (
SELECT id, parent_id, name, NULL::JSON AS children
FROM people p
WHERE NOT EXISTS ( -- only leaf nodes; see link below
SELECT 1 FROM people
WHERE parent_id = p.id
)
UNION ALL
SELECT p.id, p.parent_id, p.name, row_to_json(c) AS children
FROM cte c
JOIN people p ON p.id = c.parent_id
)
SELECT id, name, json_agg(children) AS children
FROM cte
GROUP BY 1, 2;
SQL Fiddle.
Use json_agg() to aggregate multiple branches per node in the outer SELECT
.
Minor differences to your desired result:
- This includes the
parent_id
in thechildren
column. - A single node is not wrapped into an array.
Either can be adapted, but I'd expect the result is OK for you as is.
How to identify leaf nodes:
- Select rows which are not present in other table
来源:https://stackoverflow.com/questions/34125090/reverse-aggregation-inside-of-common-table-expression