TerritoryId, Description, ParentTerritoryId, Type
--------------------------------------------------------
1, UnitedStates, null, Territory
1, John Smith, 1, Sales Rep
2, Georgia, 1, Territory
2, Jane Doe, 2, Sales Rep
2, Ann Smith, 2, Sales Rep
How can I write the following T-SQL? Let’s say I’m searching for a name like “Ann Smith”.
I’d like the resultset to look like:
1, United States, null, Territory
2, Georgia, 1, Territory
2, Ann Smith, 2, Sales Rep
Basically, I want to find a sales rep or sales reps and what organization they belong to all the way up the chain.
While it is possible to have a multilevel structure in the table ideally you would want to split your table.
one table for territory and one for sales reps.
If your sales reps can have more than one territory you would need to go to 3 tables
one for sales reps, one for territory, and a lookup table.
if you are going to do a multi level table you need each entry to have a unique id