• Dennis Post (9/10/2013)


    Looks like a job for a recursive query.

    Make up some sample data so we can help you out. (Create table + inserts + clean up)

    Or you could check out the great SQLServerCentral articles and blogs on the subject.

    I believe that it's actually a job for a simple splitter and a tiny bit of pair matching. If we can get a more clear example from the OP of even just one of these "hierarchical paths", this will be a simple job to convert to an Adjacency List "Orchard". From there, the sky is the limit because we can then slave it into a multi-tree orchard. The "Orchard" here is very similar a task I did in the past where I had to organize such data into an "Orchard" of hundreds of thousands of "Trees" all "living" on common "ground". Once it was converted to an Adjacency List, the next step was to convert it to Nested Sets and that's a whole lot easier and faster to do than it's ever been before (and, yes, it then uses an rCTE as part of the process). Please see the following article on that.

    http://www.sqlservercentral.com/articles/Hierarchy/94040/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)