Skip to content
Advertisement

How to do the total sum of a group by?

I have the following table

Name    Val
Test1   $100.00 
Test2   $200.00 
Test3   $300.00 
Test4   $400.00 
Test5   $500.00 

I am able to retrieve val by group with :

SELECT Name,sum(Val) FROM Synthese WHERE Name IN ( 'Test1' , 'Test2' , 'Test3') GROUP BY Name

Which give me the following output :

Name    Val
Test1   $100.00 
Test2   $200.00 
Test3   $300.00 

But I don’t know how to do :

sum(val)/(val(Test1)+val(Test2)+val(Test3))

Numerical example :

100/(100+200+300)
200/(100+200+300)
300/(100+200+300)

How should I do ?

Advertisement

Answer

SELECT Name, 
     sum(Val)/(SELECT sum(Val) FROM Synthese WHERE Name IN('Test1','Test2','Test3'))
FROM Synthese 
WHERE Name IN('Test1','Test2','Test3') 
GROUP BY Name
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement