## Can recursive CTE solve a Tiering problem?

 Author Message jsalisbury Forum Newbie Group: General Forum Members Points: 1 Visits: 7 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.jms Attachments SQLServerCentralCTE.txt (13 views, 3.00 KB) dwain.c SSCarpal Tunnel Group: General Forum Members Points: 4249 Visits: 6431 It might be overkill for you or it might be just what you need. Check out Jeff Moden's two articles "Hierarchies on Steroids:"Part 1Part 2Be advised that this is pretty advanced stuff so prepare yourself to be amazed. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables