Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SUM in a recursive procedure

I have a problem doing some recursive procedure. Here is the script I made which works well (except the sum I'm gonna explain later):

;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)

Here are the data I have on my table Bilangammestest:

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

Here are the result I get :

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

Here is the final result I want:

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

I tried to do a sum but I couldn't do it correctly

like image 356
XXX Avatar asked Sep 06 '26 00:09

XXX


1 Answers

Use group by

SELECT firstcolname, secondcolname, sum(thircolname) GROUP BY firstcolname, secondcolname
like image 148
Christopher Avatar answered Sep 07 '26 18:09

Christopher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!