Ordering items by hierarchy without RBAR

  • Hey all,

    I feel like I already know the answer to this question, but it is eluding me ...

    Say I have a table with two columns - ID and ParentID. Say this table has the following values:

    IDParentID

    1050

    2030

    3010

    50NULL

    Now, say I want the following output from my query:

    IDParentIDOrder

    10502

    20304

    30103

    50NULL1

    Is there any way to get this result in a single query, without needing potentially infinite recursion or using a row-by-row comparaison?

  • Sure... you could do a 4 way self join but that's hardcoding levels.

    Recursion isn't so bad here. It will do things by level, not by row.

    --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)

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

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