计算递归儿童的数量

时间:2020-06-30 05:06:19

标签: sql postgresql recursion common-table-expression

我有一些表格,如下图所示。类别通过parent_id具有层次结构树。 CategoryProducts在Category和Product之间建立了多对多关系。一种产品可以属于不同的类别。例如,“红球”可以放在“运动”和“运动”的子类别“足球”中。

如何用子类别中的产品数量计算每个类别中的产品数量?复制并不重要,例如,“体育”类别可以包含两个“红球”(一个属于自己的类别,一个属于子类别“足球”)。

表关系:

table relations

结果表:

result table

以下是无法正常运行的sql脚本:

    with recursive category_tree as (
    select c.id , c.name , c.parent_id , count(cp.id) as amo
    from temp_category_2 c
    join temp_category_products cp on cp.category_id = c.id
    where c.parent_id = null
    group by c.id
    
    union 
    
    select c2.id , c2.name , c2.parent_id , (select count(cp.id) from temp_category_products cp where cp.category_id = c2.id) as amo
    from temp_category_2 c2 
    join category_tree ct on ct.id = c2.parent_id 
)
select cat.id , cat.parent_id, 
count(cp.id) + coalesce((
    select sum(ct.amo)
    from category_tree ct 
    where ct.parent_id = cat.id
    group by ct.parent_id),0)as amount
from temp_category_2 cat
join temp_category_products cp on cp.category_id = cat.id
group by cat.id , cat.parent_id
;

表类别:

  id |   name   | parent_id | product_quantity
----+----------+-----------+------------------
  2 | a        |         1 |                3
  3 | s        |         1 |                3
  4 | d        |         2 |                3
  5 | f        |         2 |                4
  6 | g        |         3 |                2
  1 | main     |           |                0
(6 rows)

TABLE产品:

 id |  name
----+---------
  1 | example
  2 | example
  3 | example
  ...
(15 rows)

表category_products

 id | category_id | product_id
----+-------------+------------
  1 |           6 |          1
  2 |           6 |          2
  3 |           5 |          3
  4 |           5 |          4
  5 |           5 |          5
  6 |           5 |          6
  7 |           4 |          7
  8 |           4 |          8
  9 |           4 |          9
 10 |           3 |         10
 11 |           3 |         11
 12 |           3 |         12
 13 |           2 |         13
 14 |           2 |         14
 15 |           2 |         15
(15 rows)

我的查询结果(错误的结果):

 id | parent_id | amount
----+-----------+--------
  3 |         1 |      3
  5 |         2 |      4
  4 |         2 |      3
  6 |         3 |      2
  2 |         1 |      3
(5 rows)

正确的结果应该是:

id | parent_id | amount
----+-----------+--------
  1 |           |      15
  2 |         1 |      10
  3 |         1 |      5
  4 |         2 |      3
  5 |         2 |      4
  6 |         3 |      2
(6 rows)

1 个答案:

答案 0 :(得分:0)

经过一番努力,我找到了答案。 this blog上显示了一个类似的示例。

正确的sql是:

with recursive  cte(CategoryId, ParentCategoryId, c)
        AS (SELECT  c1.id,
                    c1.parent_id ,
                    (SELECT COUNT(*)
                     FROM   (SELECT DISTINCT
                                    product_id 
                             FROM   temp_category_products AS tcp
                             WHERE  tcp.category_id = c1.id
                            ) AS t1
                    ) AS c
            FROM    temp_category AS c1
            UNION ALL
            SELECT  c2.id,
                    c2.parent_id ,
                    d.c
            FROM    temp_category c2
                    INNER JOIN cte d ON c2.id = d.ParentCategoryId
           )
  SELECT  cte.CategoryId,
          cte.ParentCategoryId,
          SUM(c) AS ProductCount
  FROM    cte
  GROUP BY cte.CategoryId,
          cte.ParentCategoryId;
相关问题