<small id='VjttP'></small><noframes id='VjttP'>

          <bdo id='VjttP'></bdo><ul id='VjttP'></ul>

        <legend id='VjttP'><style id='VjttP'><dir id='VjttP'><q id='VjttP'></q></dir></style></legend>
      1. <tfoot id='VjttP'></tfoot>

      2. <i id='VjttP'><tr id='VjttP'><dt id='VjttP'><q id='VjttP'><span id='VjttP'><b id='VjttP'><form id='VjttP'><ins id='VjttP'></ins><ul id='VjttP'></ul><sub id='VjttP'></sub></form><legend id='VjttP'></legend><bdo id='VjttP'><pre id='VjttP'><center id='VjttP'></center></pre></bdo></b><th id='VjttP'></th></span></q></dt></tr></i><div id='VjttP'><tfoot id='VjttP'></tfoot><dl id='VjttP'><fieldset id='VjttP'></fieldset></dl></div>

        树结构中的递归求和

        时间:2023-10-10
          <bdo id='OPIqb'></bdo><ul id='OPIqb'></ul>
          <legend id='OPIqb'><style id='OPIqb'><dir id='OPIqb'><q id='OPIqb'></q></dir></style></legend>

            • <small id='OPIqb'></small><noframes id='OPIqb'>

                <tbody id='OPIqb'></tbody>

              <tfoot id='OPIqb'></tfoot>

                1. <i id='OPIqb'><tr id='OPIqb'><dt id='OPIqb'><q id='OPIqb'><span id='OPIqb'><b id='OPIqb'><form id='OPIqb'><ins id='OPIqb'></ins><ul id='OPIqb'></ul><sub id='OPIqb'></sub></form><legend id='OPIqb'></legend><bdo id='OPIqb'><pre id='OPIqb'><center id='OPIqb'></center></pre></bdo></b><th id='OPIqb'></th></span></q></dt></tr></i><div id='OPIqb'><tfoot id='OPIqb'></tfoot><dl id='OPIqb'><fieldset id='OPIqb'></fieldset></dl></div>

                  本文介绍了树结构中的递归求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在一个表中有一个树结构.该表是可以无限嵌套的类别树.每个类别都有一个 ProductCount 列,用于说明该类别中直接包含的产品数量(不汇总子类别).

                  I have a tree struture in a single table. The table is a tree of categories that can be nested endlessly. Each category has a ProductCount column that tells how many products are directly in the category (not summing child categories).

                  Id  | ParentId | Name      | ProductCount
                  ------------------------------------
                  1   | -1       | Cars      | 0
                  2   | -1       | Bikes     | 1
                  3   | 1        | Ford      | 10
                  4   | 3        | Mustang   | 7
                  5   | 3        | Focus     | 4
                  

                  我想做一个 sql 查询,为每一行/类别提供产品数量,包括子类别中的产品.

                  I would like to make a sql query that for each row/category gives me the number of products including the ones in the child categories.

                  上表的输出应该是

                  Id  | ParentId | Name      | ProductCount | ProductCountIncludingChildren
                  --------------------------------------------------------------------------
                  1   | -1       | Cars      | 0            | 21
                  2   | -1       | Bikes     | 1            | 1
                  3   | 1        | Ford      | 10           | 21
                  4   | 3        | Mustang   | 7            | 7
                  5   | 3        | Focus     | 4            | 4
                  

                  我知道我可能应该使用 CTE,但不能让它以应有的方式工作.

                  I know I probably should use CTE, but cant quite get it working the way it should.

                  感谢任何帮助!

                  推荐答案

                  你可以使用递归 CTE,在锚部分获取所有行,在递归部分连接获取子行.记住来自锚部分的原始 Id 别名 RootID 并在按 RootID 分组的主查询中做 sum 聚合.

                  You can use a recursive CTE where you in the anchor part get all rows and in the recursive part join to get the child rows. Remember the original Id aliased RootID from the anchor part and do sum aggregate in the main query grouped by RootID.

                  SQL 小提琴

                  MS SQL Server 2012 架构设置:

                  create table T
                  (
                    Id int primary key,
                    ParentId int,
                    Name varchar(10),
                    ProductCount int
                  );
                  
                  insert into T values
                  (1, -1, 'Cars',    0),
                  (2, -1, 'Bikes',   1),
                  (3,  1, 'Ford',    10),
                  (4,  3, 'Mustang', 7),
                  (5,  3, 'Focus',   4);
                  
                  create index IX_T_ParentID on T(ParentID) include(ProductCount, Id);
                  

                  查询 1:

                  with C as
                  (
                    select T.Id,
                           T.ProductCount,
                           T.Id as RootID
                    from T
                    union all
                    select T.Id,
                           T.ProductCount,
                           C.RootID
                    from T
                      inner join C 
                        on T.ParentId = C.Id
                  )
                  select T.Id,
                         T.ParentId,
                         T.Name,
                         T.ProductCount,
                         S.ProductCountIncludingChildren
                  from T
                    inner join (
                               select RootID,
                                      sum(ProductCount) as ProductCountIncludingChildren
                               from C
                               group by RootID
                               ) as S
                      on T.Id = S.RootID
                  order by T.Id
                  option (maxrecursion 0)
                  

                  结果:

                  | ID | PARENTID |    NAME | PRODUCTCOUNT | PRODUCTCOUNTINCLUDINGCHILDREN |
                  |----|----------|---------|--------------|-------------------------------|
                  |  1 |       -1 |    Cars |            0 |                            21 |
                  |  2 |       -1 |   Bikes |            1 |                             1 |
                  |  3 |        1 |    Ford |           10 |                            21 |
                  |  4 |        3 | Mustang |            7 |                             7 |
                  |  5 |        3 |   Focus |            4 |                             4 |
                  

                  这篇关于树结构中的递归求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:计数表中值的连续出现次数 下一篇:列名或提供的值数量与表定义不匹配

                  相关文章

                2. <tfoot id='2g1Ce'></tfoot>
                3. <small id='2g1Ce'></small><noframes id='2g1Ce'>

                4. <legend id='2g1Ce'><style id='2g1Ce'><dir id='2g1Ce'><q id='2g1Ce'></q></dir></style></legend>
                  • <bdo id='2g1Ce'></bdo><ul id='2g1Ce'></ul>

                      <i id='2g1Ce'><tr id='2g1Ce'><dt id='2g1Ce'><q id='2g1Ce'><span id='2g1Ce'><b id='2g1Ce'><form id='2g1Ce'><ins id='2g1Ce'></ins><ul id='2g1Ce'></ul><sub id='2g1Ce'></sub></form><legend id='2g1Ce'></legend><bdo id='2g1Ce'><pre id='2g1Ce'><center id='2g1Ce'></center></pre></bdo></b><th id='2g1Ce'></th></span></q></dt></tr></i><div id='2g1Ce'><tfoot id='2g1Ce'></tfoot><dl id='2g1Ce'><fieldset id='2g1Ce'></fieldset></dl></div>