[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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.

1 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

reedsster1Author Commented:
Not quite what I needed but helpful none the less.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now