Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6187747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:06:42+00:00 2026-05-24T02:06:42+00:00

I have a table Locations(id, type, code, parentid) . The type can be COUNTRY/STATE/REGION/TOWN.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:06:43+00:00Added an answer on May 24, 2026 at 2:06 am

    Here’s an example of a CTE approach:

    declare @t table (id int, type varchar(10), name varchar(50), parentid int)
    insert @t
              select 1, 'COUNTRY', 'AU', null
    union all select 2, 'STATE', 'Guiness State', 1
    union all select 3, 'STATE', 'Pilsner State', 1
    union all select 4, 'REGION', 'Heineken Region', 2
    union all select 5, 'REGION', 'Bud Light Region', 3
    union all select 6, 'TOWN', 'Hoegaarden  Town', 2
    union all select 7, 'TOWN', 'Corona Town', 2
    union all select 8, 'TOWN', 'Leffe Town', 3
    
    ; with  recursed as
            (
            select  *
            from    @t as root
            where   root.name = 'Guiness State'
            union all
            select  child.*
            from    recursed as parent
            join    @t as child
            on      parent.id = child.parentid
            )
    select  *
    from    recursed
    

    Code sample at SE Data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of locations with latitude, longitude and the U.S. state fields.
I have a database table which link locations together; a location can be in
I have 3 tables event, location, event_location. Event can have multiple locations. Location table
I have a geo_locations table that looks like this: country | city | postalCode
I have 2 tables product and history product table: id name type price location
I will try and explain this the best way I can. I have a
I have this code: public byte[] SerializeToBlob() { using (var buffer = new MemoryStream())
I have this code <?php session_start(); if (isset($_GET[cmd])) $cmd = $_GET[cmd]; else die(You should
I have this code: db = C:\Dokumente und Einstellungen\hom\Anwendungsdaten\BayWotch4\Neuer Ordner\baywotch.db5 TextExportFile = C:\Dokumente und
I have the following code that works: <script type=text/javascript> $(document).ready(function() { // Initialise the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.