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

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.

reedsster1Asked:
Who is Participating?
 
ralmadaCommented:
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

0
 
reedsster1Author Commented:
Not quite what I needed but helpful none the less.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.