Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Hierarchical Child Records with Multiple Parents

$
0
0
Hello,

In my limited experience I have used a cte to write simple hierarchical result sets but this one is a bit more complicated for me. My child data records have a primary parent record, a secondary and tertiary (no more than that). This dataset will be bound to a tree for user interaction. So as mentioned in the past I had one parent and one child but this time I need to "repeat" the recursion for each child record to be recorded under each potential parent.

Data Child records:

FoodNameID | FoodName | FoodShortName | FoodGroupID | FoodGroupID2 | FoodGroupID3
   56743          |   Alumelle   |     Omelette        |      1001          |      1007           |      1009

Data Parent Records:
FoodGroupID | FoodGroupName | FoodGroupDesc
   1001            |     Breakfast         | Big Meal
   1007            |     High Protein    | dasfsdsdf
   1009            |     Eggerific          | qerrewqer

One thought I did have was to have a one to many relational table such as FoodName_FoodGroup with said structure:

FoodNameId | FoodGroupId
   56743         |    1001
   56743         |    1007
   56743         |    1009

If using this "join" table then the query would be pretty straight forward.



Ultimately I would like to learn / use the industry best practice which is somewhat subjective but sometimes there are real obvious ones like join table vs rbar cursors.

Thank You
JB
 

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>