Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
postgresql统计子项的数量_Sql_Postgresql_Tree_Count_Children - Fatal编程技术网

postgresql统计子项的数量

postgresql统计子项的数量,sql,postgresql,tree,count,children,Sql,Postgresql,Tree,Count,Children,这棵树有无限的深度。例如: +----+-------+--------------------+ | id | name | parent_id | value | +----+-------+--------------------+ | 1 | test1 | | 0 | | 2 | test2 | 1 | 0 | | 3 | test3 | 2 | 5 | | 4 | test4 | 1

这棵树有无限的深度。例如:

+----+-------+--------------------+
| id | name  | parent_id |  value |
+----+-------+--------------------+
|  1 | test1 |           |    0   |
|  2 | test2 |     1     |    0   |
|  3 | test3 |     2     |    5   |
|  4 | test4 |     1     |    0   |
|  5 | test5 |     4     |    5   |
|  6 | test6 |     4     |    0   |
|  7 | test7 |     6     |    10  |
+----+-------+--------------------+
我想得到一个人孩子的总价值。 就这样,

+----+-------+--------------------+
| id | name  | parent_id |  value |
+----+-------+--------------------+
|  1 | test1 |           |    20  |  =  test2.value + test4.value
|  2 | test2 |     1     |    5   |  =  test3.value
|  3 | test3 |     2     |    5   |  
|  4 | test4 |     1     |    15  |  =  test5.value + test6.value
|  5 | test5 |     4     |    5   |
|  6 | test6 |     4     |    10  |  =  test7.value
|  7 | test7 |     6     |    10  |
+----+-------+--------------------+

有什么建议吗?谢谢

下面是一个递归查询,希望能解决您的问题:

WITH RECURSIVE tree (id, parent_id, cnt) AS (
    -- start from bottom-level entries
    SELECT id, parent_id, 0::bigint AS cnt
    FROM tbl t
    WHERE NOT EXISTS (
        SELECT id
        FROM tbl
        WHERE parent_id = t.id
    )

    UNION ALL

    -- join the next level, add the number of children to that of already counted ones
    SELECT t.id, t.parent_id, tree.cnt + (
            SELECT count(id) 
            FROM tbl 
            WHERE parent_id = t.id
        )
    FROM tbl t JOIN tree ON t.id = tree.parent_id
)
SELECT tree.id, max(tree.cnt) AS number_of_children
FROM tree 
-- use the JOIN if you need additional columns from tbl
-- JOIN tbl ON tree.id = tbl.id 
-- use the WHERE clause if you want to see root nodes only
-- WHERE parent_id IS NULL
GROUP BY tree.id
ORDER BY tree.id
;

我也做了一个测试。

见F.35.1.5。此处的connectby函数示例:


在不了解其他需求的情况下,我无法确定正确的模式是什么,但您的实现是在数据库中存储树的一种非常简单的方法。一些设计模式被发明出来,使得做各种聪明的事情变得更加容易,包括你问的问题。例如,查看物化路径设计模式。参见或其他参考资料。@hims056:什么不清楚?按照
parent\u id
链接,对
count
值进行递归求和。@hims056第一个表是我拥有的,第二个表是我想要创建视图的内容。我快速搜索了一下,发现这篇文章几乎就是你的使用案例。。。可能是重复的谢谢你的回答。我犯了一个错误,我真的想得到总价值,但不是计数。对不起,恐怕您的实现不正确。请考虑一棵树,有三个孩子的根,两个第一个有两个孩子,最后一个没有孩子。根据您的查询,根目录有5个子目录(子目录),而实际上它有7个子目录。