Recursive sum in tree structure

后端 未结 5 2024
走了就别回头了
走了就别回头了 2020-12-01 03:05

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 product

5条回答
  •  一个人的身影
    2020-12-01 03:50

    This is the same concept as Tom's answer, but less code (and way faster).

    with cte as
    (
      select v.Id, v.ParentId, v.Name, v.ProductCount, 
      cast('/' + cast(v.Id as varchar) + '/' as varchar) Node
      from Vehicle v
      where ParentId = -1
      union all
      select v.Id, v.ParentId, v.Name, v.ProductCount,  
      cast(c.Node + CAST(v.Id as varchar) + '/' as varchar)
      from Vehicle v
      join cte c on v.ParentId = c.Id
    )
    
    select c1.Id, c1.ParentId, c1.Name, c1.ProductCount, 
    c1.ProductCount + SUM(isnull(c2.ProductCount, 0)) ProductCountIncludingChildren
    from cte c1
    left outer join cte c2 on c1.Node <> c2.Node and left(c2.Node, LEN(c1.Node)) = c1.Node
    group by c1.Id, c1.ParentId, c1.Name, c1.ProductCount
    order by c1.Id
    

    SQL Fiddle (I added some extra data rows for testing)

提交回复
热议问题