Hi, I have a problem that I have a solution for, but I can't help but thinking a recursive CTE (or pivot) would solve the problem more elegantly.
Here's what it does
---Each client has tiers and tier limits assigned to them (my example is 5 tiers)
---There's a parent/child relationships between clients. Each parent has one or more children.
---The input file is reported at the child level, yet the tiering doesn't take place until after the quantities are aggregated to the Parent level.
---The Tier rules are as follows. Quantity is allocated from tier1 thru tier(n), applying as much quantity into each tier as the limits allow before moving to the next tier. Once Tier1 is full, you can start applying quantity to Tier2, and so on.
Attached is my T-SQL, thanks in advance for the help.