Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 公共表表达式内部的反向聚合_Sql_Json_Postgresql_Common Table Expression_Recursive Query - Fatal编程技术网

Sql 公共表表达式内部的反向聚合

Sql 公共表表达式内部的反向聚合,sql,json,postgresql,common-table-expression,recursive-query,Sql,Json,Postgresql,Common Table Expression,Recursive Query,我希望下面的查询返回所有人及其各自的孩子 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

我希望下面的查询返回所有人及其各自的孩子

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;
但实际上情况正好相反。我想不出一种不需要额外的CTE就能正确嵌套的方法。有办法吗

示例数据

+----+-------+-----------+
| id | name  | parent_id |
+----+-------+-----------+
|  1 | Adam  | null      |
|  2 | Abel  | 1         |
|  3 | Cain  | 1         |
|  4 | Enoch | 3         |
+----+-------+-----------+
结果

+----+-------+--------------------------------------------------------------------------+
| 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}} |
+----+-------+--------------------------------------------------------------------------+
+----+-------+----------------------------------------------------------------------------------------------------------------------+
| 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                                                                                                                 |
+----+-------+----------------------------------------------------------------------------------------------------------------------+
预期结果

+----+-------+--------------------------------------------------------------------------+
| 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}} |
+----+-------+--------------------------------------------------------------------------+
+----+-------+----------------------------------------------------------------------------------------------------------------------+
| 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                                                                                                                 |
+----+-------+----------------------------------------------------------------------------------------------------------------------+

此rCTE从另一侧穿过树:

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;

用于聚合外部
选择中每个节点的多个分支
与预期结果的细微差异:

  • 这包括
    children
    列中的
    parent\u id
  • 单个节点未包装到数组中
两者都可以调整,但我希望结果对你来说都是可以的

如何识别叶节点:


您能添加一些数据样本吗?从这个样本中可以得到什么样的结果?谢谢,好主意。我补充了你要求的内容。