I am using SQL Server.
I have a table Groups with two integer columns:
MEGR_KEY
MEGR_KEY1
MEGR_KEY
is primary key of group. Each group can have sub groups.
For example - I have a group 1195:
MEGR_KEY
= 1195
There are subgroups of 1195:
MEGR_KEY = 9484
MEGR_KEY1 = 1195
and
MEGR_KEY = 7494
MEGR_KEY1 = 1195
Basically MEGR_KEY1
is telling, which group is a parent.
The problem I have is, how to find all MEGR_KEY
subgroups hierarchically, given only root group name? Let's say (from previous example) there is 1195 root group. There are already two subgroups: 7494 and 9484. Now, those two subgroups can also be parent groups to some other groups. So, I have to find rows where MEGR_KEY1
= 7494 OR MEGR_KEY1
= 9484. How to find all subgroups if group number (MEGR_KEY) is given? I have a problem here writing query for this.