Combining data from several rows in SQL conditionaly based on a tree level.

Posted on 2009-12-23
Last Modified: 2012-08-13
I have a SQL Database where my  query retrieves data from the database like so:

TreeLevel      Food Type                           InventoryNbr      SeqNbr
1                      Flat Bread                                 -                      37
2                      With Swiss Cheese              300                   38
2                      Condiment                                -                      39
3                       With Mayo                           301                   40
3                       With Mustard                       302                     41

The Tree Level column indicates applications that need to be grouped together. Food type indicates the type of food.  InventoryNbr is my internal inventory number, and SeqNbr is a sequencing number used to sort the database.

When the InventoryNbr is blank, it indicates that one of the food types in the tree level after it needs to go with it.  I'm trying to write a SQL query that concatenates these into single rows with very little luck.  My desired effect is:
TreeLevel      Food Type                  InventoryNbr      SeqNbr     FullFoodType
1                      Flat Bread                                 -                      37                     -
2                      With Swiss Cheese              300                   38     Flat Bread With Swiss Cheese
2                      Condiment                                -                      39                     -
3                       With Mayo                           301                   40     Flat Bread With Swiss Cheese Condiment With Mayo
3                       With Mustard                       302                     41     Flat Bread With Swiss Cheese Condiment With Mustard

The deepest tree level is 11 deep.  

Is this even possible or am I chasing my tail?  
I apologize for the poor alignment and appreciate any help.

Question by:reedsster1
    LVL 41

    Accepted Solution

    You need a parentid column to link them Check the below:
    Your table should look like this:
    TreeLevel      ParentID 	Food Type                      InventoryNbr      SeqNbr
    1              	0		Flat Bread                                 -         37 
    2              	37		With Swiss Cheese              300                   38
    2              	38		Condiment                                -           39
    3              	39		With Mayo                           301              40
    3              	39		With Mustard                       302               41
    Then you can run this query:
    ;with CTE as (
    	select TreeLevel, FoodType, InventoryNbr, SeqNbr from yourtable
    	where TreeLevel = 1
    	union all
    	select a.Treelevel, b.FoodType + ' ' + a.Foodtype, b.InventoryNbr, b.SeqNbr from yourtable a
    	inner join CTE b on a.ParentID = b.SeqNbr
    select * from CTE

    Open in new window


    Author Closing Comment

    Not quite what I needed but helpful none the less.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now