Can recursive CTE solve a Tiering problem?

  • 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

  • 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[/url]

    Part 2[/url]

    Be advised that this is pretty advanced stuff so prepare yourself to be amazed.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply