I have a table Locations(id, type, code, parentid).
The type can be COUNTRY/STATE/REGION/TOWN. So this table holds hierarchical geographic data.
Country will have many states, state many regions and regions many towns.
I need to list out descendent records for a given parent record. For example I need all records under Country Australia. The parent record in this case will be
10, COUNTRY, AU, null)
I understand a recursive CTE is the way to go but not sure how to.
thanks
Here’s an example of a CTE approach:
Code sample at SE Data.