Mr. Aaron. I am sorry, your solution didn't match in my case.
Example for your solution :
A = 1
B = 1
C = 1
D = 1
E = 1
F = 1
G = 4
G have 4 candy. Its too much for G.
In my case, the solution is :
A = 1
B = 1
C = 1
D = 1
E = 2
F = 2
G = 2
The extra candy is given to three child.
Do you have the other solution? I need function in postgresql for my case.
Because my loop is too slow.
Btw thanks for your solution.
But it looks like Ross got you the code to fix the problem. Check what he provided.
==================================================================
Aaron Bono
Aranya Software Technologies, Inc.
http://www.aranya.com
==================================================================