I am trying to define a sql table for groups and subgroups, and so far my design is as follows
groupID groupname parentgroupID
1 group1 null
2 group2 null
3 subgroup1.1 1
4 subgroup1.1.1 3
I think I am on the right track. Now I would like to retreive data for subgroup 1.1.1 in order to be able to display it in a bussiness tier
group1 > subgroup 1.1 > subgroup 1.1.1
Should I do this in tsql, or I am suppose to do a loop in asp.net in order to get all available parent groups?
This question is both sql and asp.net question so I will post it in both categories.
Thanks for participating
You can solve this – provided your on SQL Server 2005 or newer with a recursive CTE:
This should give you the results something like this:
Any additional formatting should be done in your front-end (web, client/server).
The
Levelcolumn is an extra bonus, telling you what level in the hierarchy each row is positioned on.