sql - 如何递归求和父/子层次结构

标签 sql sql-server tsql

我有一个如下所示的数据表:

UnitID  UnitParentID    ScoreCount  ScoreSum   Level
7112                       72          292       1 
7120    7112               72          308       2
7139    7112               24           82       2 
7150    7112              166          586       2
23682   7112              104          414       2
7100    7112              272         1016       2
23691   7112               94          300       2
23696   7112               24           80       2
23700   23696             184          594       3
23694   23691              24           64       3
23689   7120               88          390       3
7148    23696             112          420       3
7126    7120               32          132       3
7094    7120               96          332       3
7098    7094               64          240       4
23687   7094               16           62       4

我想要做的是从最低层次结构到最高层次结构递归添加,以便下面的数字汇总到其父级中。因此,父级会将任何子级添加到其现有值中,一直到树的顶部。

在此示例中,最后两行将保持不变,因为它们没有子行。单元 ID 7094 的得分为 96(基础)+ 64 + 16(2 个子),新总数为 176(得分和的逻辑相同)。 3 级的其他人将保持不变,因为他们没有 child 。我相信我需要从底部开始,以便上面的层对于任何 child 都有正确的值。

如果有人能给我指出一个好的资源,让我可以学习如何实现这一目标,我将非常感激。

最佳答案

WITH CTE AS ( SELECT 7112 unitid , NULL UnitParentId,72 ScoreCount,292 ScoreSum,1 Level UNION ALL SELECT 7120 unitid ,7112 UnitParentId,72 ScoreCount,308 ScoreSum,2 Level UNION ALL SELECT 7139 unitid ,7112 UnitParentId,24 ScoreCount,82 ScoreSum,2 Level UNION ALL SELECT 7150 unitid ,7112 UnitParentId,166 ScoreCount,586 ScoreSum,2 Level UNION ALL SELECT 23682 unitid ,7112 UnitParentId,104 ScoreCount,414 ScoreSum,2 Level UNION ALL SELECT 7100 unitid ,7112 UnitParentId,272 ScoreCount,1016 ScoreSum,2 Level UNION ALL SELECT 23691 unitid ,7112 UnitParentId,94 ScoreCount,300 ScoreSum,2 Level UNION ALL SELECT 23696 unitid ,7112 UnitParentId,24 ScoreCount,80 ScoreSum,2 Level UNION ALL SELECT 23700 unitid ,23696 UnitParentId,184 ScoreCount,594 ScoreSum,3 Level UNION ALL SELECT 23694 unitid ,23691 UnitParentId,24 ScoreCount,64 ScoreSum,3 Level UNION ALL SELECT 23689 unitid ,7120 UnitParentId,88 ScoreCount,390 ScoreSum,3 Level UNION ALL SELECT 7148 unitid ,23696 UnitParentId,112 ScoreCount,420 ScoreSum,3 Level UNION ALL SELECT 7126 unitid ,7120 UnitParentId,32 ScoreCount,132 ScoreSum,3 Level UNION ALL SELECT 7094 unitid ,7120 UnitParentId,96 ScoreCount,332 ScoreSum,3 Level UNION ALL SELECT 7098 unitid ,7094 UnitParentId,64 ScoreCount,240 ScoreSum,4 Level UNION ALL SELECT 23687 unitid ,7094 UnitParentId,16 ScoreCount,62 ScoreSum,4 Level ), RECURSIVECTE AS ( SELECT unitid, CONVERT(NVARCHAR(MAX),convert(nvarchar(20),unitid)) PARENTLIST, ScoreCount FROM CTE WHERE UnitParentId IS NULL

UNION ALL

SELECT C.unitid, CONVERT(NVARCHAR(MAX),convert(nvarchar(20),R.PARENTLIST) + ',' + convert(nvarchar(20),C.unitid)), C.ScoreCount
FROM RECURSIVECTE R INNER JOIN CTE C ON R.unitid = C.UnitParentId )

SELECT C.unitid, R.ScoreCount FROM CTE C CROSS APPLY ( SELECT SUM(ScoreCount) ScoreCount FROM RECURSIVECTE R WHERE CHARINDEX(convert(nvarchar(20),C.UNITID), R.PARENTLIST,0) > 0 ) R

关于sql - 如何递归求和父/子层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30978757/

相关文章:

mysql - 重复和无序的数字

android - 如何使我的 SQlite 查询不区分大小写?

mySQL 使用最大日期更新值

sql-server - 如何从脚本创建 SQL Server 2008 数据库

sql-server - 如何知道 MS SQL Server 中存储过程的执行状态

java - 使用 Hibernate SQL 查询在日期范围内搜索文档

sql - 当 value > 1 时如何获取多行

mysql - 将 SQL 转储转换为 JSON?

sql - 普通 SQL 与方言

sql - 什么是 SQL/PSM?它与其他版本的 SQL(如 T-SQL)有何不同?