递归过程中的 SUM

SUM in a recursive procedure

我在执行一些递归过程时遇到问题。 这是我制作的脚本,效果很好(除了我稍后要解释的总和):

;WITH RESULT (MOTHER, CHILD, QUANTITY) as
(
    select Mother, Child, CONVERT(Numeric(10,0), Quantity) as Quantity 
    from bilangammestest 

    union all 

    select M.mother, R.Child, CONVERT(Numeric(10,0), M.quantity * R.Quantity) as Quantity 
    from Result R 
    INNER JOIN bilangammestest M ON M.Child = R.Mother
)

select * from result
where mother not in (select child from bilangammestest)

这是我在 table Bilangammestest 上的数据:

Z A 1    
Z Y 1    
A B 2    
Y B 2    
B C 3

这是我得到的结果:

Z A 1    
Z Y 1    
Z C 6    
Z C 6    
Z B 2    
Z B 2

这是我想要的最终结果:

Z A 1    
Z Y 1    
Z C 12    
Z B 4

我尝试做一个 sum 但我做不正确

使用group by

SELECT firstcolname, secondcolname, sum(thircolname) GROUP BY firstcolname, secondcolname

最后一个查询应该是:

select MOTHER, CHILD, sum(quantity) quantity
from result
where mother not in (select child from bilangammestest )
group by MOTHER, CHILD