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

  • SEARCH
  • Home
  • 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 9039269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:41:23+00:00 2026-06-16T09:41:23+00:00

I have this data in a recursive table, and I want to flatten this

  • 0

I have this data in a recursive table, and I want to flatten this hierarchy information. I have accomplished this by doing a query but I was wondering if there is a better way to get the same result.

Data that I have:

CREATE TABLE [dbo].[ElementosGeograficos_](
    [IdElement] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [IdParentElement] [int] NULL,
    [IdLevel] [int] NOT NULL,

    CONSTRAINT [PK_ElementosGeograficos_] 
      PRIMARY KEY CLUSTERED ([IdElement] ASC)
GO

 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Colombia',null,1
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Venezuela',null,1
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Cundinamarca',1,2
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Antioquia',1,2
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Valle',1,2
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Distrito Capital',2,2
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Bogota',3,3
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Medellin',4,3
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Cali',5,3
 insert ElementosGeograficos_ ( Name,IdParentElement,IdLevel )  select 'Caracaas',6,3

Query result that I need:

 - NUM  1           2                   3 
 - 1    Colombia    Null                Null 
 - 2    Venezuela   Null                Null
 - 3    Colombia    Cundinamarca        Null 
 - 4    Colombia    Antioquia           Null
 - 5    Colombia    Valle               Null 
 - 6    Venezuela   Distrito Capital    Null
 - 7    Colombia    Cundinamarca        Bogota 
 - 8    Colombia    Antioquia           Medellin
 - 9    Colombia    Valle               Cali 
 - 10   Venezuela   Distrito Capital    Caracaas

Query that I have:

SELECT 
    EG1.IdElementoGeografico, EG1.Nombre 'Pais', 
    EG2.Nombre 'Departamento', EG3.Nombre 'Ciudad'
FROM 
    ElementosGeograficos_ EG1 
LEFT JOIN 
    ElementosGeograficos_ EG2 ON EG2.IdElementoGeografico = EG1.IdElementoPadre 
LEFT JOIN 
    ElementosGeograficos_ EG3 ON EG3.IdElementoGeografico = EG2.IdElementoPadre
WHERE 
    EG1.IdNivelGeografico = 1

UNION

SELECT 
    EG1.IdElementoGeografico, EG2.Nombre 'Pais', 
    EG1.Nombre 'Departamento', EG3.Nombre 'Ciudad'
FROM 
    ElementosGeograficos_ EG1 
LEFT JOIN 
    ElementosGeograficos_ EG2 ON EG2.IdElementoGeografico = EG1.IdElementoPadre 
LEFT JOIN 
    ElementosGeograficos_ EG3 ON EG3.IdElementoGeografico = EG2.IdElementoPadre
WHERE 
    EG1.IdNivelGeografico = 2

UNION

SELECT 
    EG1.IdElementoGeografico, EG3.Nombre 'Pais',
    EG2.Nombre 'Departamento', EG1.Nombre 'Ciudad'
FROM 
    ElementosGeograficos_ EG1 
LEFT JOIN 
    ElementosGeograficos_ EG2 ON EG2.IdElementoGeografico = EG1.IdElementoPadre 
LEFT JOIN 
    ElementosGeograficos_ EG3 ON EG3.IdElementoGeografico = EG2.IdElementoPadre
WHERE 
    EG1.IdNivelGeografico = 3
  • 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-06-16T09:41:24+00:00Added an answer on June 16, 2026 at 9:41 am

    Since you seem to have two known levels you can just use a self-join, coalesce and a case statement

    SELECT 
       first.IdElement num,
       COALESCE(third.name, second.Name, first.Name) [1],
       CASE WHEN Third.Name IS NOT NULL THEN second.Name 
            WHEN SECOND.Name IS NOT NULL THEN first.Name
       END as [2],
       CASE WHEN Third.Name IS NOT NULL and second.Name IS NOT NULL THEN First.Name 
      END [3]
    FROM
      ElementosGeograficos_ first
      LEFT JOIN ElementosGeograficos_ second
      ON first.IDParentElement = second.IdElement
      LEFT JOIN ElementosGeograficos_ third
      ON second.IDParentElement = third.IdElement
    

    DEMO

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

Sidebar

Related Questions

Hy there, i have some tree-structured data in my sqlite-table and want to delete
I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr
I have this data: /blabla/blabla (abs,def) /yxz I use this regex (.*)(?:\(([^$]*)\))?\n But it
I have this data in a table, for instance, id name parent parent_id 1
I have a table with some persistent data in it. Now when I query
I have this problem. I have a table (below) of groups. It's a recursive
I have a recursive hierarchy in a relational database, this reflects teams and their
Let’s say I have table/list like this n=3 in this case, but n can
I have this data in cell A1: Majestic Properties Design District, LLC, Ste. 101,

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.