Comment hiérarchiser récursivement la hiérarchie parent / enfant

J'ai une table de données qui ressemble à ceci:

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 

Ce que je cherche à faire est récursivement append de la hiérarchie la plus basse au plus haut de sorte que les numéros ci-dessous se déroulent dans leur parent. Ainsi, un parent appendait à ses valeurs existantes des enfants, en haut de l'tree.

Dans cet exemple, les deux dernières lignes restront inchangées car elles n'ont pas d'enfants. L'ID unité 7094 aurait un score de 96 (base) + 64 + 16 (2 enfants) pour un nouveau total de 176 (et la même logique pour scoresum). Les autres au niveau 3 restraient inchangés car ils n'ont pas d'enfants. Je crois que je dois commencer en bas pour que les couches ci-dessus aient les valeurs correctes pour tous les enfants.

Si quelqu'un pouvait me diriger vers une bonne source où je pourrais apprendre comment accomplir ceci, je serais très reconnaissant.


AVEC CTE AS
(
SELECT 7112 unitid, NULL UnitParentId, 72 ScoreCount, 292 ScoreSum, 1 Niveau UNION TOUS
SELECT 7120 unitid, 7112 UnitParentId, 72 ScoreCount, 308 ScoreSum, 2 Niveau UNION TOUS
SELECT 7139 unitid, 7112 UnitParentId, 24 ScoreCount, 82 ScoreSum, 2 Niveau UNION TOUS
SELECT 7150 unitéid, 7112 UnitParentId, 166 ScoreCount, 586 ScoreSum, 2 niveau UNION ALL
SELECT 23682 unitid, 7112 UnitParentId, 104 ScoreCount, 414 ScoreSum, 2 Niveau UNION TOUS
SELECT 7100 unitéid, 7112 UnitParentId, 272 ScoreCount, 1016 ScoreSum, 2 niveau UNION ALL
SELECT 23691 unitid, 7112 UnitParentId, 94 ScoreCount, 300 ScoreSum, 2 Niveau UNION TOUS
SÉLECTIONNER 23696 unitid, 7112 UnitParentId, 24 ScoreCount, 80 ScoreSum, 2 Niveau UNION TOUS
SELECT 23700 unitéid, 23696 UnitParentId, 184 ScoreCount, 594 ScoreSum, 3 Niveau UNION ALL
SEULEMENT 23694 unitid, 23691 UnitParentId, 24 ScoreCount, 64 ScoreSum, 3 niveau UNION ALL
SELECT 23689 unitid, 7120 UnitParentId, 88 ScoreCount, 390 ScoreSum, 3 Niveau UNION TOUS
SEULEMENT 7148 unitid, 23696 UnitParentId, 112 ScoreCount, 420 ScoreSum, 3 niveau UNION ALL
SELECT 7126 unitid, 7120 UnitéParentId, 32 ScoreCount, 132 ScoreSum, 3 Niveau UNION ALL
SELECT 7094 unitid, 7120 UnitParentId, 96 ScoreCount, 332 ScoreSum, 3 Niveau UNION ALL
SELECT 7098 unitéid, 7094 UnitParentId, 64 ScoreCount, 240 ScoreSum, 4 niveau UNION ALL
SELECT 23687 unitéid, 7094 UnitParentId, 16 ScoreCount, 62 ScoreSum, 4 niveau
),
RECURSIVECTE AS
(
SELECT unitid, CONVERT (NVARCHAR (MAX), convertir (nvarchar (20), unitid)) PARENTLIST, ScoreCount
DU CTE
O Unit UnitParentId IS NULL

UNION TOUS

SELECT C.unitid, CONVERT (NVARCHAR (MAX), convertir (nvarchar (20), R.PARENTLIST) + ',' + convertir (nvarchar (20), C.unitid)), C.ScoreCount
DE RECURSIVECTE R
INNER JOIN CTE C SUR R.unitid = C.UnitParentId
)

SELECT C.unitid, R.ScoreCount
DE CTE C
CROSS APPLIQUE
(
SELECT SUM (ScoreCount) ScoreCount
DE RECURSIVECTE R
O CHAR CHARINDEX (convertir (nvarchar (20), C.UNITID), R.PARENTLIST, 0)> 0
R