Home Forums SQL Server 2012 SQL Server 2012 - T-SQL How to get up to 5 hierarchy levels of data from original (parent) to derived (children) product table. RE: How to get up to 5 hierarchy levels of data from original (parent) to derived (children) product table.

  • Greg Edwards-268690 - Thursday, January 4, 2018 6:41 AM

    ChrisM@Work - Thursday, January 4, 2018 5:51 AM

    aaron.reese - Thursday, January 4, 2018 4:26 AM

    Google for Recursive CTE - basically a CTE that references itself.  You can set a level 0 in the CTE and then a derived level as the derived level of the parent +1

    If you want to present the hierarchy as a single row you will need to use STUFF..FOR XML and PIVOT()

    Have a go at some code and post back if you get stuck.

    This is confusing - he's posted a recursive CTE as his sample code.

    When you look at the results - with parent listed under itself for different children - it still needs work.
    At least from the way our business would want to see it.
    No indented BOM in the ERP would reflect this.

    Of course - but I'd describe this as display, or at least getting the result set out in a form which won't take too much effort to display.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden