Oracle Recursive Join - Many to Many Relationship

user1698923

I've been trying for a few hours without success, to recursively join a table to itself. It's probably simplest to see the problem in this SQL Fiddle, rather than me trying to explain the linking relationship:

http://sqlfiddle.com/#!4/367c3/14

In the above example, the actual data might nest up to 5 layers deep, perhaps more. I'd like to write a query that handles joins to any number of layers.

From doing some searches, it seems that it's possible to recursively join the data with CONNECT BY PRIOR, and other methods. I just haven't been able to get it to work :(. It'd be awesome if one of you guru's could show me how it's done.

michaelgulak

Copying your tables from SQL Fiddle for others to see in case the external link becomes invalid in the future:

* DEVICE TABLE
DEVICEID DEVICENAME
-------------------
1        Device1
2        Device2
3        Device3
4        Device4
5        Device5
6        Device6
7        Device7
8        Device8
9        Device9
10       Device10

* CONNECTION TABLE
IDPARENT IDCHILD
----------------
1        2
3        4
4        5
6        7
7        8
4        8
4        8
5        9

I don't know what data exactly you want to query from the tables, but if you are just concerned with the self-"join" using a connect by prior, here you go:

select distinct d.deviceid, d.devicename, connect_by_root(deviceid) rootdeviceid
from device d
    left join connection c on (d.deviceid = c.idchild)
connect by prior d.deviceid = c.idparent
start with c.idparent is null
order by d.deviceid

Note that there are 11 rows returned by this query given the table you provided -- this is because device 8 actually has two root nodes in the hierarchy you provided, 3 and 6.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive relationship on a many to many table

From Dev

Recursive Many to many relationship with Yii

From Dev

Related name for recursive many to many relationship not working

From Dev

Oracle SQL Developer - JOIN on 2 queries with a one-to-many relationship

From Dev

JPQL left outer join on many to many relationship

From Dev

Laravel Complicated inner join on many to many relationship

From Dev

Inner Join Between Many to Many Relationship Models

From Dev

Ebean many to many relationship with join fails

From Dev

Rails: many to many relationship join table design

From Dev

How to JOIN tables in many-to-many relationship

From Dev

MySQL, join many to many relationship query

From Dev

Sqlalchemy one to many relationship join?

From Dev

Many to one relationship, unwanted JOIN

From Dev

Exclusive join on has many relationship

From Dev

How to join many to many relationship on max value for relationship field

From Dev

One to many recursive relationship with Code First

From Dev

GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

From Dev

GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

From Dev

CTE recursive query for two tables that has a many to many relationship

From Dev

Django ORM: Recursive many-to-many field join

From Dev

Mysql Left Join (1 to many relationship)

From Dev

Naming convention for join tables, in a many to many relationship, in Laravel

From Dev

CakePHP deleteAll for many to many relationship does not remove join records

From Dev

Prevent duplicate entries in a join table in a many-to-many relationship in JPA

From Dev

Django many to many relationship with join table-specific fields

From Dev

Force inner join with many-to-many relationship entity framework

From Dev

Join EF code first many to many relationship tables

From Dev

Core Data Join Table Many to Many Relationship - Object Creation

From Dev

Why create join table to make many to many relationship in Rails 3

Related Related

  1. 1

    Recursive relationship on a many to many table

  2. 2

    Recursive Many to many relationship with Yii

  3. 3

    Related name for recursive many to many relationship not working

  4. 4

    Oracle SQL Developer - JOIN on 2 queries with a one-to-many relationship

  5. 5

    JPQL left outer join on many to many relationship

  6. 6

    Laravel Complicated inner join on many to many relationship

  7. 7

    Inner Join Between Many to Many Relationship Models

  8. 8

    Ebean many to many relationship with join fails

  9. 9

    Rails: many to many relationship join table design

  10. 10

    How to JOIN tables in many-to-many relationship

  11. 11

    MySQL, join many to many relationship query

  12. 12

    Sqlalchemy one to many relationship join?

  13. 13

    Many to one relationship, unwanted JOIN

  14. 14

    Exclusive join on has many relationship

  15. 15

    How to join many to many relationship on max value for relationship field

  16. 16

    One to many recursive relationship with Code First

  17. 17

    GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

  18. 18

    GraphDiff and EF6.1 – Recursive Many-to-Many Relationship

  19. 19

    CTE recursive query for two tables that has a many to many relationship

  20. 20

    Django ORM: Recursive many-to-many field join

  21. 21

    Mysql Left Join (1 to many relationship)

  22. 22

    Naming convention for join tables, in a many to many relationship, in Laravel

  23. 23

    CakePHP deleteAll for many to many relationship does not remove join records

  24. 24

    Prevent duplicate entries in a join table in a many-to-many relationship in JPA

  25. 25

    Django many to many relationship with join table-specific fields

  26. 26

    Force inner join with many-to-many relationship entity framework

  27. 27

    Join EF code first many to many relationship tables

  28. 28

    Core Data Join Table Many to Many Relationship - Object Creation

  29. 29

    Why create join table to make many to many relationship in Rails 3

HotTag

Archive