ls_dev
ls_dev

Reputation: 191

Joining two Hierarchical queries to form larger Hierarchy

I have researched this and know I'm not the first to ask but I can't seem to get my head around it. I have created a simple example that I think will help me crack it if someone can provide the missing link!

I have a table of areas that contains continents and countries in a hierarchy.

I also have a table of places that contains cities and landmarks in a hierarchy. This table contains an area id column to join to the areas table.

    create table areas
(
  id            NUMBER not null,
  name          VARCHAR2(200) not null,
  parent_id     NUMBER
);

-- Top Level
Insert into areas (id, name)
 Values (1, 'Europe');
Insert into areas (id, name)
 Values (2, 'Americas');
Insert into areas (id, name)
 Values (3, 'Asia ex Japan');
Insert into areas (id, name)
 Values (4, 'Japan');

 -- Jurisdictions
Insert into areas (id, name, parent_id)
 Values (5, 'UK', 1);
Insert into areas (id, name, parent_id)
 Values (7, 'France', 1);
Insert into areas (id, name, parent_id)
 Values (6, 'Germany', 1);
Insert into areas (id, name, parent_id)
 Values (8, 'Italy', 1);
Insert into areas (id, name, parent_id)
 Values (9, 'US', 2);
Insert into areas (id, name, parent_id)
 Values (10, 'Australia', 3);
Insert into areas (id, name, parent_id)
 Values (11, 'New Zealand', 3);

create table places
(
  id            NUMBER not null,
  name          VARCHAR2(200) not null,
  area_id       NUMBER,
  parent_id     NUMBER
);

Insert into places (id, name, area_id, parent_id)
 Values (1, 'London', 5, NULL);
Insert into places (id, name, area_id, parent_id)
 Values (2, 'Bath', 5, NULL);
Insert into places (id, name, area_id, parent_id)
 Values (3, 'Liverpool', 5, NULL);
Insert into places (id, name, area_id, parent_id)
 Values (4, 'Paris', 7, NULL);
Insert into places (id, name, area_id, parent_id)
 Values (5, 'New York', 9, NULL);
Insert into places (id, name, area_id, parent_id)
 Values (6, 'Chicago', 9, NULL);
Insert into places (id, name, area_id, parent_id)
 Values (7, 'Kings Cross', 5, 1);
Insert into places (id, name, area_id, parent_id)
 Values (8, 'Tower of London', 5, 1);

I can query these tables independently like this:

 SELECT a.*, level FROM areas a
start with parent_id is null
connect by prior id = parent_id

SELECT p.*, level FROM places p
start with parent_id is null
connect by prior id = parent_id

Is someone able to show me the last step to join these into one query with four levels? I've been working with Oracle for years but somehow this never came up!

If there was no connect by prior in the places table, just a list of cities with an area id, would this be easier?

Thank you

Upvotes: 4

Views: 148

Answers (1)

Sanders the Softwarer
Sanders the Softwarer

Reputation: 2496

Is it what you need?

with src as (
  select 'A' type, a.id, a.name, a.parent_id, null area_id from areas a
  union all
  select 'P', -p.id id, p.name, -p.parent_id parent_id, area_id from places p)
select 
  src.*, level
from 
  src
start with 
  type = 'A' and parent_id is null
connect by 
  parent_id = prior id or 
  parent_id is null and area_id = prior id

Upvotes: 2

Related Questions