Apologies if this does not fit the proper format for this site, as it is a somewhat general question.
I have an application that sits on top of a SQL database, and need to handle a process which is very much like a linear algebra problem, whereby an amount a (any number between 0-1B) needs to get distributed among n entities (e), based on a few parameters (rank, weight, min/max requirements) set at the entity level.
Example:
a = 100
entity weight min max
----------------------------------------------
X 0.25 10 40
Y 0.75 40 60
...
In this example, 25 (a * X[weight]) would go to entity X, and 75 would go to entity Y; however, 75 exceeds Y[max], so the remaining 15 need to go to another entity (in this case, X, since it stays at or under X[max]).
Intuitively, this is in iterative process. In a real example, there would be more entities, so more iterations would be necessary. SQL is not designed to iteration. I am looking for a way to better handle this in a set based method.
What I am looking for is something along the lines of:
a statistical method that I can use to minimize the number of iterations I need to make, or even better, a way that this can be distilled into a formula?
alternatively, maybe there is a way to store some of the data in a static way to minimize the steps needed to make the calculations on the fly?
Creating a lookup table, whereby for each entity there could be a stored outcome for min/max ranges, based on the other entities (they are in groups of 10 or less).