Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Can recursive CTE solve a Tiering problem? Expand / Collapse
Posted Friday, March 7, 2014 3:36 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, May 20, 2014 11:10 AM
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.

  Post Attachments 
SQLServerCentralCTE.txt (13 views, 3.83 KB)
Post #1548918
Posted Sunday, March 9, 2014 9:07 PM

Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, February 24, 2016 6:28 AM
Points: 3,977, Visits: 6,431
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 1
Part 2

Be 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
Post #1549097
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse