Joining two tables with many to many relationship in sql

k.chinni66

I have two tables with many to many relationship. I need to join them and get the matched records.

Table 1

Column1 | column 2| column 3|
1|p1|1.0
1|p1|1.1
1|p1|1.2

Table 2

Column1 | column 2| column 3|
1|p1|2.0
1|p1|2.1
1|p1|2.2

Now I want the result as

1|p1|1.0|2.0
1|p1|1.1|2.1
1|p1|1.2|2.2

I mean column1 and column2 matching and showing values from both columns for column3

Edit 1: I have one issue after trying MT0 query. I am very much satisfied with his answer but still need some changes to be done:

Table 1

Column1 | column 2| column 3|
1|p1|1.0
1|p1|1.1
1|p1|1.2

Table 2

Column1 | column 2| column 3|
1|p1|1.0
1|p1|1.2

Now I want the result as

1|p1|1.0|1.0
1|p1|1.1|NULL
1|p1|1.2|1.2

But I am getting as

1|p1|1.0|1.0
1|p1|1.1|1.2
1|p1|1.2|NULL

Please do some help on this

MT0

If you have unequal numbers of rows for each partition then you can do:

Oracle Setup:

CREATE TABLE table1 ( col1, col2, col3 ) AS
SELECT 1, 'P1', '1.0' FROM DUAL UNION ALL
SELECT 1, 'P1', '1.1' FROM DUAL UNION ALL
SELECT 1, 'P1', '1.2' FROM DUAL UNION ALL
SELECT 1, 'P2', '1.0' FROM DUAL UNION ALL
SELECT 1, 'P2', '1.2' FROM DUAL UNION ALL
SELECT 2, 'P1', '1.0' FROM DUAL;

CREATE TABLE table2 ( col1, col2, col3 ) AS
SELECT 1, 'P1', '2.0' FROM DUAL UNION ALL
SELECT 1, 'P1', '2.1' FROM DUAL UNION ALL
SELECT 1, 'P1', '2.2' FROM DUAL UNION ALL
SELECT 1, 'P2', '2.1' FROM DUAL UNION ALL
SELECT 2, 'P1', '2.0' FROM DUAL UNION ALL
SELECT 2, 'P1', '2.1' FROM DUAL;

Query:

SELECT COALESCE( t1.col1, t2.col1 ) AS col1,
       COALESCE( t1.col2, t2.col2 ) AS col2,
       t1.col3 AS t1col3,
       t2.col3 AS t2col3
FROM   (
         SELECT t.*,
                ROW_NUMBER() OVER ( PARTITION BY col1, col2
                                    ORDER BY col3 ) AS rn
         FROM   table1 t
       ) t1
       FULL OUTER JOIN
       (
         SELECT t.*,
                ROW_NUMBER() OVER ( PARTITION BY col1, col2
                                    ORDER BY col3 ) AS rn
         FROM   table2 t
       ) t2
       ON ( t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.RN = t2.rn )
ORDER BY col1, col2, t1col3 NULLS LAST, t2col3 NULLS LAST;

Output:

      COL1 COL2 T1COL3 T2COL3
---------- ---- ------ ------
         1 P1   1.0    2.0    
         1 P1   1.1    2.1    
         1 P1   1.2    2.2    
         1 P2   1.0    2.1    
         1 P2   1.2           
         2 P1   1.0    2.0    
         2 P1          2.1    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Joining two tables with many to many relationship in sql

From Dev

Joining a table to two one-to-many relationship tables in SQL Server

From Dev

Joining many tables in SQL

From Dev

Joining two sql tables with a one to many relationship, but want the max of the second table

From Dev

Joining multiple tables with one to many relationship

From Dev

SQL Joining many-to-many key relationship tables makes it hard to find the distinct sum

From Dev

Many-to-Many Relationship between two tables in two different databases

From Dev

Query two tables with one to many relationship

From Dev

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

From Dev

Connecting two tables that have a many-to-many relationship in powerpivot

From Dev

SQL Many-to-many relationship between 3 tables

From Dev

many to many relationship with three tables (sql-alchemy)

From Dev

SQL many to many relationship table

From Dev

Querying a many to many relationship in SQL

From Dev

Many to Many Relationship over multiple tables

From Dev

Many to Many Relationship over multiple tables

From Dev

Entity Framework with many to many relationship generetad tables

From Dev

How to JOIN tables in many-to-many relationship

From Dev

Joining and filtering one-to-many relationship

From Dev

Joining and filtering one-to-many relationship

From Dev

Joining results from many tables in postgresql

From Dev

Joining of two tables in SQL Server

From Dev

Joining two tables in SQL Server

From Dev

Joining two tables on SQL Server

From Dev

SQL joining two tables and counting

From Dev

SQL QUERY: Joining of two tables

From Dev

Joining two sql tables and grouping

From Dev

SQL joining two tables with a LEFTJOIN

From Dev

Joining of two tables in SQL Server

Related Related

  1. 1

    Joining two tables with many to many relationship in sql

  2. 2

    Joining a table to two one-to-many relationship tables in SQL Server

  3. 3

    Joining many tables in SQL

  4. 4

    Joining two sql tables with a one to many relationship, but want the max of the second table

  5. 5

    Joining multiple tables with one to many relationship

  6. 6

    SQL Joining many-to-many key relationship tables makes it hard to find the distinct sum

  7. 7

    Many-to-Many Relationship between two tables in two different databases

  8. 8

    Query two tables with one to many relationship

  9. 9

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

  10. 10

    Connecting two tables that have a many-to-many relationship in powerpivot

  11. 11

    SQL Many-to-many relationship between 3 tables

  12. 12

    many to many relationship with three tables (sql-alchemy)

  13. 13

    SQL many to many relationship table

  14. 14

    Querying a many to many relationship in SQL

  15. 15

    Many to Many Relationship over multiple tables

  16. 16

    Many to Many Relationship over multiple tables

  17. 17

    Entity Framework with many to many relationship generetad tables

  18. 18

    How to JOIN tables in many-to-many relationship

  19. 19

    Joining and filtering one-to-many relationship

  20. 20

    Joining and filtering one-to-many relationship

  21. 21

    Joining results from many tables in postgresql

  22. 22

    Joining of two tables in SQL Server

  23. 23

    Joining two tables in SQL Server

  24. 24

    Joining two tables on SQL Server

  25. 25

    SQL joining two tables and counting

  26. 26

    SQL QUERY: Joining of two tables

  27. 27

    Joining two sql tables and grouping

  28. 28

    SQL joining two tables with a LEFTJOIN

  29. 29

    Joining of two tables in SQL Server

HotTag

Archive