用递归方法计算子对象的数量

mepcadol  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(606)

我有如下图所示的表格。categoryproducts在类别和产品之间建立了多对多的关系。一种产品可以分为不同的类别。例如“红球”可以放在“运动”和“运动”的子类别“足球”。
如何使用子类别的产品数量计算每个类别的产品数量?重复并不重要,例如,类别“运动”可以包含两个“红球”(一个来自自己的类别,一个来自子类别“足球”)。
表关系:

结果表:

以下是无法正常工作的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)

餐桌产品:

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

表类别产品:

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)
dfty9e19

dfty9e191#

经过一番努力,我找到了答案。类似的例子也出现在这个博客上。
正确的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;

相关问题