160 likes | 313 Views
Hierarchies Evan Koch Senior Architect ekoch@askcts.com. Agenda. Traditional Approach Nested Sets Approach HierarchyID Approach Questions. Traditional Approach - Model. Larry. Matt. David. Mark. Melinda. Jennifer. Craig. Tom. Wendy. Sample Org Chart.
E N D
Agenda • Traditional Approach • Nested Sets Approach • HierarchyID Approach • Questions
Traditional Approach - Model Larry Matt David Mark Melinda Jennifer Craig Tom Wendy Sample Org Chart
Nested Sets Approach - Model David Melinda Matt Mark Jennifer Craig Tom Wendy Larry
Nested Sets Approach - Model 1 Larry 18 Matt 8 David Mark 17 2 7 11 12 9 Melinda Jennifer 16 13 3 Craig 4 5 Tom 6 10 14 Wendy 15 Sample Org Chart
Nested Sets Approach - Model David 8 Melinda 9 10 11 Matt Mark Jennifer Craig Tom 1 2 3 4 5 6 7 Wendy 12 13 16 14 15 17 18 Larry
Nested Sets Approach - Table Table Schema
HierarchyIDApproach - Model Larry / Matt /1/ David /2/ Mark /3/ Melinda /2/1/ Jennifer /3/1/ Craig /1/1/ Tom /1/2/ Wendy /3/1/1/ Sample Org Chart
HierarchyIDApproach - Model David /2/ Melinda /2/1/ Matt /1/ Mark/3/ Jennifer /3/1/ Craig /1/1/ Tom /1/2/ Wendy /3/1/1/ Larry /
HierarchyIDApproach - Table Table Schema
HierarchyID Functions • GetAncestor • GetDescendant • GetLevel • GetRoot • IsDescendantOf • Parse • GetReparentedValue • ToString • Read * • Write *
Queries • Retrieve a subtree given the root • Find all leaf nodes • Retrieve a single path • Find immediate subordinates • Find immediate peers
References Joe Celko’s SQL For Smarties – Advanced SQL Programming http://www.amazon.com/Joe-Celkos-SQL-Smarties-Fourth/dp/0123820227/ref=sr_1_1?s=books&ie=UTF8&qid=1342876431&sr=1-1&keywords=sql+for+smarties