0

I have the following (simplified) table:

group_id member_id
1 2
2 3
3 4
5 4
6 4
7 8

It's to manage groups and their members, while a group can also be the member of a group. What I want is to get all groups where a specific ID is a member of, either directly or via another group.

So for the example above I would want to have all groups that the member with the ID 4 is a member of and the result should be (without the brackets):

(sorry for the broken 2nd table, it always look good in the preview)

| group_id |

| 1 (via group 2)| | 2 (via group 3)| |3 (directly)| |5 (directly)| |6 (directly|

I am honestly a bit lost right now and I am not sure if researching recursive queries is the right path or if there's an easier solution.

Thanks for your help.

Core_F
  • 3,262
  • 3
  • 28
  • 51
  • 1
    Yes, you need a recursive CTE to query a hierarchical data structure using a single query. – Gordon Linoff Jul 07 '21 at 11:51
  • I guess, something similar was solved here https://stackoverflow.com/questions/68132482/mssql-how-can-i-achieve-this-possibly-with-a-recursive-function#68132482 – Sergey Jul 07 '21 at 11:58

0 Answers0